当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js v8.serializer.writeUint32()用法及代码示例


这个v8.serializer.writeUint32()方法用于写入原始的 32 位无符号整数。这个函数主要用在自定义里面serializer._writeHostObject()

用法

v8.serializer.writeUint32(value)

参数

  • value − 此参数接受将写入内部缓冲区的 32 位整数的输入。

该函数不返回任何内容,而是将传递的值写入内部缓冲区。

例子1

创建一个名为“的文件writeUint32.js"并复制以下代码片段。创建文件后,使用命令"node writeUint32.js"来运行这段代码。

// v8.deserialize() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Initializing the v8 Serializer object
const serializer = new v8.Serializer();

// Adding 32-bit integer to internal buffer.
// This will return undefined since it only adds to buffer
console.log(serializer.writeUint32(7869));
// Releasing and printing the internal buffer
console.log(serializer.releaseBuffer());

输出

C:\home\node>> node writeUint32.js
undefined
<Buffer bd 3d>

例子2

让我们再看一个例子

// v8.serializer.writeUint32() Demo Example

// Importing v8 module
const v8 = require('v8');
const serializer = new v8.Serializer();

// Calling v8.serializer.writeUint32()
console.log(serializer.releaseBuffer());
// Writing the same 32-bit integer twice
serializer.writeUint32(7869);
serializer.writeUint32(7869);
// Adding the above value to internal buffer
console.log(serializer.releaseBuffer());

// Writing the same 32-bit integer again
serializer.writeUint32(7869);
// console.log(serializer.releaseBuffer());

console.log("Reading the same value after adding to buffer");
// Calling v8.deserializer.readUint32()
const deserializer = new
v8.Deserializer(serializer.releaseBuffer());
// Deserializing the internal buffer
console.log(deserializer.readUint32());

输出

C:\home\node>> node writeUint32.js
<Buffer >
<Buffer bd 3d bd 3d>
Reading the same value after adding to buffer
7869

相关用法


注:本文由纯净天空筛选整理自Mayank Agarwal大神的英文原创作品 Node.js – v8.serializer.writeUint32() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。