2016-03-29 65 views
0

我正在處理一個二進制文件,我試圖通過NodeJs解析並存儲爲變量。我有一個緩衝區中的文件。根據需求文件的這部分文件是UTF-32。nodejs UTF-32的緩衝區toString函數

41 00 00 00 55 00 00 00 54 00 00 00 4F 00 00 00 
31 00 00 00 45 00 00 00 30 00 00 00 33 00 00 00 
38 00 00 00 31 00 00 00 31 00 00 00 36 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 

var string_UserName = data.toString('utf-8', 96, 179); 
console.log('User Name: ' + string_UserName); 
//User Name: A U T O 1 E 0 3 8 1 1 6 

var string_UserName = data.toString('utf-16le', 96, 179); 
console.log('User Name: ' + string_UserName); 
//User Name: A U T O 1 E 0 3 8 1 1 6 

var string_UserName = data.toString('utf-32le', 96, 179); 
console.log('User Name: ' + string_UserName); 
//buffer.js:387 
     throw new TypeError('Unknown encoding: ' + encoding); 
    ^
TypeError: Unknown encoding: utf-32le 
    at Buffer.slowToString (buffer.js:387:17) 
    at Buffer.toString (buffer.js:399:31) 

根據Node Documentation for Buffer沒有內置轉換爲字符串utf-32的東西。

是否有一個NPM模塊可以擴展BUFFER以允許此轉換,或者是否需要編寫一個buffer.prototype.toString()函數來擴展toString(),以允許進行此轉換?如果有的話,是否有人已經有一個他們使用?


這是文件頭256/FF字節的HEX轉儲。

AF 03 00 00 D0 00 00 00 16 81 03 1E 0A 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
41 00 00 00 55 00 00 00 54 00 00 00 4F 00 00 00 
31 00 00 00 45 00 00 00 30 00 00 00 33 00 00 00 
38 00 00 00 31 00 00 00 31 00 00 00 36 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 32 30 31 36 30 31 32 32 31 32 33 35 
34 32 30 30 30 30 30 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

使用@弗拉德的答案

Iconv = require('iconv').Iconv; 
var convertUTF32 = new Iconv('UTF-32', 'UTF-8'); 
var string_UserName = convertUTF32.convert(data.slice(96, 179)); 
console.log('User Name: ' + string_UserName); 

後,我從ICONV得到一個錯誤:

C:\nodeCode\node_modules\iconv\lib\iconv.js:145 
      throw errnoException('EINVAL', 'Incomplete character sequence.'); 
     ^

Error: Incomplete character sequence. 
    at errnoException (C:\nodeCode\node_modules\iconv\lib\iconv.js:169:13) 
    at Object.convert (C:\nodeCode\node_modules\iconv\lib\iconv.js:145:17) 
    at Iconv.convert (C:\nodeCode\node_modules\iconv\lib\iconv.js:59:12) 
    at C:\nodeCode\metaProc.js:49:37 
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:404:3) 
    at fs.js:312:11 
    at nextTickCallbackWith0Args (node.js:456:9) 
    at process._tickCallback (node.js:385:13) 

有人認爲,這是因爲data.slice(96, 179)不是4的倍數,但我可以不明白,自96以來,如果第一個字節和179是4字節組(176,177,178,179)的最後一個字節。 180將是下一個字節的開始而不是結束。

任何幫助,將不勝感激。


終於搞清楚我做錯了什麼。 @Vlad大部分都是對的。我沒有//TRANSLIT//IGNORE那裏,並且不得不改變結束索引1,因爲buffer.slice看起來像Half-Closed Interval [96,180)而不是inclusive [96,179]就像我想的那樣。

我真正需要的是什麼:

Iconv = require('iconv').Iconv; 
var iconv32 = new Iconv('UTF-32LE', 'UTF-8//TRANSLIT//IGNORE'); 
var string4_UserName = iconv32.convert(data.slice(96,180)).toString('utf-8'); 
console.log('User Name: ' + string4_UserName); 

回答

0

是的,有經常用於緩衝解碼的iconv npm module

按照其自述,你可以嘗試:

var Iconv = require('iconv').Iconv; 
var iconv = new Iconv('UTF-32LE', 'UTF-8'); 
var string_UserName = iconv.convert(data.slice(96, 179)).toString('utf8'); 
console.log('User Name: ' + string_UserName); 

讓我知道它是否適合你。

+0

當我嘗試npm install iconv時,它告訴我我找不到Python。 > node-gyp rebuild C:\ nodeCode \ node_modules \ iconv>如果未定義npm_config_node_gyp(節點「C:\ Program Files \ nodejs \ node_modules \ npm \ bin \ node-gyp-bin \\ .. \ ..」 \ node_modules \ node-gyp \ bin \ node-gyp.js「rebuild)else(node rebuild) gyp ERR!配置錯誤 gyp ERR!堆棧錯誤:找不到Python可執行文件「python」,你可以設置PYTHON env變量。 我試圖找到一些可以做到的事情,而無需引入編譯器。 – shaun

+0

@shaun看看http://stackoverflow.com/a/21366601/2727317 我想你正在使用Windows –

+0

在修改我的VS 2015副本以包含C++之後。我終於得到了npm install iconv來完成。但是,當我使用 'Iconv = require('iconv')。語言Iconv; var convertUTF32 = new Iconv('UTF-32LE','UTF-8'); var string_UserName = convertUTF32.convert(data.slice(96,179)); console.log('用戶名:'+ string_UserName);' 我得到的只是一個錯誤。 '錯誤:不完整的字符序列.' 當我將其更改爲'var string_UserName = convertUTF32.convert(data.slice(96,179))。toString('utf8'); – shaun