1.string转buffer

var str = 'hello,world';
var buffer = Buffer.from(str)

buffer的值为

转回字符串

buffer.toString()

hello,world

2.使用 Uint8Array

var array = new Uint8Array(new ArrayBuffer(str.length));
for (var i = 0, il = str.length; i < il; i++) {
    var value = str.charCodeAt(i);
    array[i] = value > 0xFF ? 0x20 : value;
}
var arrBuffer=array.buffer

arrBuffer的值为
ArrayBuffer {

byteLength: 11
}

转成buffer

Buffer.from(array.buffer)

转成字符串

(Buffer.from(array.buffer)).toString()

(Buffer.from(array.buffer)).toString()

hello,world

标签: none

添加新评论