如何在nodejs缓冲区中存储整数?

发布于 2024-12-14 02:54:21 字数 296 浏览 0 评论 0原文

Nodejs Buffer 非常强大。然而,它似乎适合存储字符串。构造函数采用字符串、字节数组或要分配的字节大小。

我使用的是 Node.js 0.4.12 版本,我想在缓冲区中存储一个整数。不是integer.toString(),而是整数的实际字节。有没有一种简单的方法可以做到这一点,而无需循环整数并进行一些位调整?我可以做到这一点,但我觉得这是其他人在某个时候一定遇到过的问题。

The nodejs Buffer is pretty swell. However, it seems to be geared towards storing strings. The constructors either take a string, an array of bytes, or a size of bytes to allocate.

I am using version 0.4.12 of Node.js, and I want to store an integer in a buffer. Not integer.toString(), but the actual bytes of the integer. Is there an easy way to do this without looping over the integer and doing some bit-twiddling? I could do that, but I feel like this is a problem someone else must have faced at some time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Saygoodbye 2024-12-21 02:54:21
var buf = new Buffer(4);
buf.writeUInt8(0x3, 0);

http://nodejs.org/docs/v0.6.0/api/buffers.html# buffer.writeUInt8

var buf = new Buffer(4);
buf.writeUInt8(0x3, 0);

http://nodejs.org/docs/v0.6.0/api/buffers.html#buffer.writeUInt8

海之角 2024-12-21 02:54:21

对于最新版本的 Node,这要容易得多。以下是 2 字节无符号整数的示例:

let buf = Buffer.allocUnsafe(2);
buf.writeUInt16BE(1234);  // Big endian

或者 4 字节有符号整数的示例:

let buf = Buffer.allocUnsafe(4);  // Init buffer without writing all data to zeros
buf.writeInt32LE(-123456);  // Little endian this time..

在节点 v0.5.5 中添加了不同的 writeInt 函数。

查看这些文档以更好地理解:
缓冲区
writeUInt16BE/LE
writeUIntBE/LE
allocUnsafe

With more recent versions of Node this is much easier. Here's an example for a 2 byte unsigned integer:

let buf = Buffer.allocUnsafe(2);
buf.writeUInt16BE(1234);  // Big endian

Or for a 4 byte signed integer:

let buf = Buffer.allocUnsafe(4);  // Init buffer without writing all data to zeros
buf.writeInt32LE(-123456);  // Little endian this time..

The different writeInt functions were added in node v0.5.5.

Have a look at these docs for a better understanding:
Buffer
writeUInt16BE/LE
writeUIntBE/LE
allocUnsafe

筑梦 2024-12-21 02:54:21

由于它不是内置的 0.4.12 你可以使用这样的东西:

var integer = 1000;
var length = Math.ceil((Math.log(integer)/Math.log(2))/8); // How much byte to store integer in the buffer
var buffer = new Buffer(length);
var arr = []; // Use to create the binary representation of the integer

while (integer > 0) {
    var temp = integer % 2;
    arr.push(temp);
    integer = Math.floor(integer/2);
}

console.log(arr);

var counter = 0;
var total = 0;

for (var i = 0,j = arr.length; i < j; i++) {
   if (counter % 8 == 0 && counter > 0) { // Do we have a byte full ?
       buffer[length - 1] = total;
       total = 0;
       counter = 0;
       length--;      
   }

   if (arr[i] == 1) { // bit is set
      total += Math.pow(2, counter);
   }
   counter++;
}

buffer[0] = total;

console.log(buffer);


/* OUTPUT :

racar $ node test_node2.js 
[ 0, 0, 0, 1, 0, 1, 1, 1, 1, 1 ]
<Buffer 03 e8>

*/

Since it's not builtin 0.4.12 you could use something like this:

var integer = 1000;
var length = Math.ceil((Math.log(integer)/Math.log(2))/8); // How much byte to store integer in the buffer
var buffer = new Buffer(length);
var arr = []; // Use to create the binary representation of the integer

while (integer > 0) {
    var temp = integer % 2;
    arr.push(temp);
    integer = Math.floor(integer/2);
}

console.log(arr);

var counter = 0;
var total = 0;

for (var i = 0,j = arr.length; i < j; i++) {
   if (counter % 8 == 0 && counter > 0) { // Do we have a byte full ?
       buffer[length - 1] = total;
       total = 0;
       counter = 0;
       length--;      
   }

   if (arr[i] == 1) { // bit is set
      total += Math.pow(2, counter);
   }
   counter++;
}

buffer[0] = total;

console.log(buffer);


/* OUTPUT :

racar $ node test_node2.js 
[ 0, 0, 0, 1, 0, 1, 1, 1, 1, 1 ]
<Buffer 03 e8>

*/
无力看清 2024-12-21 02:54:21

这是非常有效的方法,但使用了一些“位调整”

// Filled 8 bits number
const fillByte = 0xff;
function bufferFromInt(num){
    const buffArr = [];
    do {
        buffArr.push(fillByte & num);
    } while(num >>= 8);
    return Buffer.from(buffArr.reverse())
}

This is something very efficient but uses some "bit twiddling"

// Filled 8 bits number
const fillByte = 0xff;
function bufferFromInt(num){
    const buffArr = [];
    do {
        buffArr.push(fillByte & num);
    } while(num >>= 8);
    return Buffer.from(buffArr.reverse())
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文