
- 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.writeInt16BE() 方法
Buffer.writeInt16BE() 方法用於以大端序形式將帶符號的 16 位整數寫入緩衝區的指定偏移量。寫入緩衝區的整數以二進位制補碼形式表示帶符號的值。
16 位帶符號整數可以儲存的值範圍為 -32768 到 32767。
二進位制補碼是二進位制數上的數學運算。要獲取任何給定二進位制數的二進位制補碼,步驟如下:
二進位制數:101011
步驟 1 - 首先找到二進位制數 101011 的反碼。要獲取反碼,需要將 1 變成 0,將 0 變成 1。因此,上述二進位制數的反碼為:010100。
步驟 2 - 現在,對步驟 1 中的反碼加 1。
I.e. 010100 + 1 = 010101. So 010101 is the two's complement of binary number 101011.
語法
以下是 Node.JS Buffer.writeInt16BE() 方法的語法:
buf.writeInt16BE(value[, offset])
引數
此方法接受兩個引數。下面解釋了這兩個引數。
value - (必填) 要寫入緩衝區的帶符號 16 位整數。
offset - 指示開始讀取位置的偏移量。偏移量大於或等於 0,並且小於或等於 buffer.length-2。預設值為 0。
返回值
buffer.writeInt16BE() 方法寫入給定值並返回偏移量加上寫入的位元組數。
示例
要建立緩衝區,我們將使用 NodeJS Buffer.alloc() 方法:
const buffer = Buffer.alloc(10); buffer.writeInt16BE(0x0910, 0); console.log(buffer);
輸出
我們使用的偏移量為 0。執行時,從第 0 個位置開始的值將寫入建立的緩衝區。上面建立的緩衝區長度為 10。因此,我們只能使用 0 到 8 之間的值作為偏移量。如果任何值 >8,則會報錯 ERR_OUT_OF_RANGE。
<Buffer 09 10 00 00 00 00 00 00 00 00>
示例
在此示例中,我們將使用一個大於 buffer.length - 2 的偏移量。它應該會丟擲如下錯誤。
const buffer = Buffer.alloc(10); buffer.writeInt16BE(0x0910, 9); console.log(buffer);
輸出
internal/buffer.js:83 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 <= 8. Received 9 at boundsError (internal/buffer.js:83:9) at checkBounds (internal/buffer.js:52:5) at checkInt (internal/buffer.js:71:3) at writeU_Int16BE (internal/buffer.js:829:3) at Buffer.writeInt16BE (internal/buffer.js:891:10) at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:2:8) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) { code: 'ERR_OUT_OF_RANGE' }
示例
要建立緩衝區,我們將使用 Buffer.allocUnsafe() 方法:
const buffer = Buffer.allocUnsafe(10); buffer.writeInt16BE(0x0910); console.log(buffer);
輸出
未使用偏移量,預設情況下將取值為 0。因此,上述程式碼的輸出如下所示:
<Buffer 09 10 1c 43 a5 55 00 00 08 00>
nodejs_buffer_module.htm
廣告