當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。