Skip to main content

[WebAPIs] Text Encoder

keywords: TextEncoder(), encode#

TextEncoder @ MDN WebAPI

TextEncoder 可以將字串轉換成 UTF-8 的 Unit8Array 位元組(bytes)

const encoder = new TextEncoder();
const view = encoder.encode('臺'); // Uint8Array(3) [232, 135, 186]
// 臺 = Unicode Dec(33274)
// = Unicode Hex(81FA)
// = UTF-8 Hex(E8 87 BA)
// = UTF-8 Dec(232, 135, 186)
const codePoint = '臺'.charCodeAt(); // 33274
console.log(codePoint.toString(16)); // 81fa
console.log(0xe8); // 232
console.log(0x87); // 135
console.log(0xba); // 186
Last updated on