• index {integer}

    索引操作符 [index] 可用于获取或设置 buf 中指定的 index 位置的八位字节。 该值指向单个字节,所以有效的值的范围是 0x000xFF(十六进制)、或 0255(十进制)。

    该操作符继承自 Uint8Array,所以对越界访问的行为与 Uint8Array 相同。 也就是说,当 index 为负数或大于或等于 buf.length 时,则 buf[index] 返回 undefined,而如果 index 为负数或 >= buf.length 时,则 buf[index] = value 不会修改该 buffer。

    1. // 拷贝 ASCII 字符串到 `Buffer`,每次拷贝一个字节。
    2. // (这仅适用于只有 ASCII 字符串。通常,应使用 `Buffer.from()` 来执行此转换。)
    3. const str = 'http://nodejs.cn/';
    4. const buf = Buffer.allocUnsafe(str.length);
    5. for (let i = 0; i < str.length; i++) {
    6. buf[i] = str.charCodeAt(i);
    7. }
    8. console.log(buf.toString('utf8'));
    9. // 打印: http://nodejs.cn/