
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - Buffer.writeUInt32BE() 方法
NodeJS 的 Buffer.writeUInt32BE() 方法用於將一個無符號 32 位整數的值以大端序的形式寫入緩衝區的指定偏移量位置。如果值不是一個 32 位無符號整數,則返回 undefined。
語法
以下是 Node.JS Buffer.writeUInt32BE() 方法 的語法:
buf.writeUInt32BE(value[, offset])
引數
此方法接受兩個引數,解釋如下。
value − (必填) 要寫入緩衝區的無符號 32 位數字。
offset − 指示寫入起始位置的偏移量。偏移量必須大於等於 0,並且小於等於 buffer.length-4。預設值為 0。
返回值
buffer.writeUInt32BE() 方法寫入指定的值並返回偏移量加上寫入的位元組數。
示例
為了建立緩衝區,我們將使用 NodeJS Buffer.alloc() 方法:
const buffer = Buffer.alloc(10); buffer.writeUInt32BE(1000); console.log(buffer);
輸出
執行後,從第 0 個位置開始的值將被寫入建立的緩衝區。上面建立的緩衝區長度為 10。因此,我們只能使用 0 到 6 的偏移量值。任何大於 6 的值都會導致 ERR_OUT_OF_RANGE 錯誤。
<Buffer e8 03 00 00 00 00 00 00 00 00>
示例
在這個例子中,我們將使用緩衝區長度為 5,並且偏移量大於 buffer.length - 4。
const buffer = Buffer.alloc(5); buffer.writeUInt32BE(1000, 2); console.log(buffer);
輸出
internal/buffer.js:58 throw new ERR_OUT_OF_RANGE(type || 'offset', ^ RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 1. Received 2 at boundsError (internal/buffer.js:58:9) at checkBounds (internal/buffer.js:39:5) at checkInt (internal/buffer.js:46:3) at writeU_Int32BE (internal/buffer.js:636:3) at Buffer.writeUInt32BE (internal/buffer.js:649:10) at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8) at Module._compile (internal/modules/cjs/loader.js:816:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10) at Module.load (internal/modules/cjs/loader.js:685:32) at Function.Module._load (internal/modules/cjs/loader.js:620:12)
示例
在這個例子中,我們將建立一個緩衝區,並從一個在範圍內的偏移量開始寫入。
const buffer = Buffer.alloc(10); buffer.writeUInt32BE(123, 4); console.log(buffer);
輸出
偏移量必須在 0 到 6 之間。上述程式碼執行後的輸出如下:
<Buffer 00 00 00 00 00 00 00 7b 00 00>
nodejs_buffer_module.htm
廣告