如何从node.js缓冲区获取int

时间:2022-01-02 17:10:30

is the code:

是代码:

var time = new Buffer('506BF1E3','hex');
time.toString() //'Pk��'

0x506BF1E3 = 1349251555 unixtime;

0x506BF1E3 = 1349251555 unixtime;

How get '1349251555' form time Buffer?

如何获得'1349251555'形成时间缓冲?

3 个解决方案

#1


3  

This one will do the trick as well. Convert hex to int by calling parseInt with radix argument 16:

这个也可以做到这一点。通过使用radix参数16调用parseInt将十六进制转换为int:

var time = new Buffer('506BF1E3','hex');
console.log(parseInt(time.toString('hex'), 16));

#2


0  

this will do the trick:

这样做可以解决问题:

var time = new Buffer('506BF1E3','hex');
console.log(parseInt("0x"+time.toString("hex")));

#3


0  

Use this? Seems much better than parsing...

用这个?似乎比解析好得多......

time.readUInt32BE(0)

However, I think you need to make sure that this is always 4 bytes (32-bits), otherwise it will fail.

但是,我认为你需要确保这总是4个字节(32位),否则它将失败。

#1


3  

This one will do the trick as well. Convert hex to int by calling parseInt with radix argument 16:

这个也可以做到这一点。通过使用radix参数16调用parseInt将十六进制转换为int:

var time = new Buffer('506BF1E3','hex');
console.log(parseInt(time.toString('hex'), 16));

#2


0  

this will do the trick:

这样做可以解决问题:

var time = new Buffer('506BF1E3','hex');
console.log(parseInt("0x"+time.toString("hex")));

#3


0  

Use this? Seems much better than parsing...

用这个?似乎比解析好得多......

time.readUInt32BE(0)

However, I think you need to make sure that this is always 4 bytes (32-bits), otherwise it will fail.

但是,我认为你需要确保这总是4个字节(32位),否则它将失败。