- 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 - Streams
- Node.js - 文件系统
- Node.js MySQL
- Node.js - MySQL 快速入门
- Node.js - MySQL创建数据库
- Node.js - MySQL创建表
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where 子句
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB 快速入门
- Node.js - MongoDB 创建数据库
- Node.js - MongoDB 创建集合
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB 查询
- Node.js - MongoDB 排序
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js模块
- Node.js - 模块
- Node.js - 内置模块
- Node.js - utility 模块
- Node.js - Web 模块
Node.js - Buffer.writeIntBE() 方法
NodeJS Buffer.writeIntBE() 方法有助于在给定偏移量处以大端格式将字节长度、字节的值写入缓冲区。
语法
以下是Node.JS Buffer.writeIntBE()方法的语法 -
buf.writeIntBE(value, offset, bytelength)
参数
此方法接受三个参数。下面将对此进行解释。
- value − (必填)要写入缓冲区的数字。
- offset − (必需) 指示开始写入的位置的偏移量。偏移量大于或等于 0,也小于或等于 buffer.length-bytelength。默认值为 0
- byteLength − (必需) 您要写入的字节数。byteLength 必须介于 0 到 6 之间。
返回值
方法 buffer.writeIntBE() 写入给定值并返回偏移量加上写入的字节数。
例为了创建一个缓冲区,我们将使用 NodeJS Buffer.alloc() 方法 -
const buffer = Buffer.alloc(10);
buffer.writeIntBE(123, 0, 6);
console.log(buffer);
输出
我们使用的偏移量是 0,字节长度是 6。在执行时,从第 0 个位置开始的值将被写入创建的缓冲区。为上述创建的缓冲区长度为 10。因此,我们只能使用值为 0 到 4 的偏移量。如果任何值 >4,它将给出错误 ERR_OUT_OF_RANGE。
<Buffer 00 00 00 00 00 7b 00 00 00 00>
例
在此示例中,将使用字节长度> 6。它应该抛出一个错误,如下所示。
const buffer = Buffer.alloc(10);
buffer.writeIntBE(123, 0, 8);
console.log(buffer);
输出
internal/buffer.js:83
throw new ERR_OUT_OF_RANGE(type || 'offset',
^
RangeError [ERR_OUT_OF_RANGE]: The value of "byteLength" is out of range. It must be >= 1 and <= 6. Received 8
at boundsError (internal/buffer.js:83:9)
at Buffer.writeIntBE (internal/buffer.js:854:3)
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)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
code: 'ERR_OUT_OF_RANGE'
}
throw new ERR_OUT_OF_RANGE(type || 'offset',
^
RangeError [ERR_OUT_OF_RANGE]: The value of "byteLength" is out of range. It must be >= 1 and <= 6. Received 8
at boundsError (internal/buffer.js:83:9)
at Buffer.writeIntBE (internal/buffer.js:854:3)
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)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
code: 'ERR_OUT_OF_RANGE'
}
例
在此示例中,将使用大于 buffer.length − bytelength 的偏移量。
const buffer = Buffer.alloc(10);
buffer.writeIntBE(123, 8, 3);
console.log(buffer);
输出
偏移量必须介于 0 到 7 之间。由于我们使用了 8,它将抛出如下所示的错误 -
internal/buffer.js:83
throw new ERR_OUT_OF_RANGE(type || 'offset',
^
RangeError [ERR_OUT_OF_RANGE]: The value of "byteLength" is out of range. It must be >= 1 and <= 6. Received 8
at boundsError (internal/buffer.js:83:9)
PS C:\nodejsProject> node src/testbuffer.js
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 <= 7. Received 8
at boundsError (internal/buffer.js:83:9)
at checkBounds (internal/buffer.js:52:5)
at checkInt (internal/buffer.js:71:3)
at writeU_Int24BE (internal/buffer.js:817:3)
at Buffer.writeIntBE (internal/buffer.js:875:12)
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'
}
throw new ERR_OUT_OF_RANGE(type || 'offset',
^
RangeError [ERR_OUT_OF_RANGE]: The value of "byteLength" is out of range. It must be >= 1 and <= 6. Received 8
at boundsError (internal/buffer.js:83:9)
PS C:\nodejsProject> node src/testbuffer.js
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 <= 7. Received 8
at boundsError (internal/buffer.js:83:9)
at checkBounds (internal/buffer.js:52:5)
at checkInt (internal/buffer.js:71:3)
at writeU_Int24BE (internal/buffer.js:817:3)
at Buffer.writeIntBE (internal/buffer.js:875:12)
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'
}