2017-04-13 29 views
0

我從一個文件作爲緩衝區獲取數據,然後將其轉換成一個長的代碼示例面臨的問題是從文件中獲取4個字節,並將其轉換爲timestamp

var fs = require('fs'); 

fs.open('24.mkt', 'r', function(status, fd) { 
    if (status) { 
    console.log(status.message); 
    return; 
    } 
    var buffer = new Buffer(4); 
    fs.read(fd, buffer, 0, 4, 0, function(err, num) { 
    console.log(buffer.values()); 
    }); 
}); 

文件鏈接 - >https://archive.org/download/kamo_24/24.mkt

前4個字節包含4個字節

+0

的可能的複製// stackoverflow.com/questions/30911185/javascript-reading-3-bytes-buffer-as-an-integer) –

回答

0

您可能需要使用(的bufferreadUInt32BE and/or readUInt32LE,以緩衝值轉換爲數目龍時間戳。

您也可以嘗試使用node-bigintnode-bignum在緩衝區中轉換值的數值(它可能是一個4個字節的情況下矯枉過正,但如果你需要處理一個更大的數字可能滿足的需要),都允許在類似的形式創建緩衝區(只是要注意的選項差異):

bignum.fromBuffer(buf, opts) 
// or 
bigint.fromBuffer(buf, opts) 
1

可以使用的node.js的Buffer.readInt32BE功能。它讀取在給定的順序(大或小端)4個字節到一個變量,起始於偏移參數:[:讀取3個字節緩衝器爲一個整數的JavaScript](HTTP:

// Unix timestamp now: 1492079016 
var buffer = Buffer.from([0x58, 0xEF, 0x51, 0xA8]); 
var timestamp = buffer.readInt32BE(0); 

process.stdout.write(timestamp.toString()); 
相關問題