const msg = JSON.stringify({ a: 123, b: true, c: "456" }) const strencode = (str) => { let byteArray = []; for (let i = 0; i < str.length; i++) { let charCode = str.charCodeAt(i); if (charCode <= 0x7f) { byteArray.push(charCode); } else if (charCode <= 0x7ff) { byteArray.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f)); } else if (charCode <= 0xffff) { byteArray.push(0xe0 | (charCode >> 12), 0x80 | ((charCode & 0xfc0) >> 6), 0x80 | (charCode & 0x3f)); } else { byteArray.push(0xf0 | (charCode >> 18), 0x80 | ((charCode & 0x3f000) >> 12), 0x80 | ((charCode & 0xfc0) >> 6), 0x80 | (charCode & 0x3f)); } } return byteArray; } var arr = strencode(msg) var buffer = Buffer.from(msg) console.log(buffer) for (let i = 0; i < arr.length; i++) { console.log(buffer[i], arr[i]); }