4
2

More than 3 years have passed since last update.

Node.js: 文字列をUint8Arrayに変換する方法

Posted at

Node.jsで文字列をUnit8Arrayに変換する方法です。

const str = 'Hello こんにちは'
const encoder = new TextEncoder()
const encoded = encoder.encode(str)
console.log(encoded)

実行結果:

Uint8Array(21) [
   72, 101, 108, 108, 111,  32,
  227, 129, 147, 227, 130, 147,
  227, 129, 171, 227, 129, 161,
  227, 129, 175
]

簡単な説明

  • TextEncoderencodeメソッドに文字列を渡すと、Unit8Arrayが返ってくる。
  • UTF-8のみサポートしている。
  • TextEncoderのインターフェイスはWHATWG Encoding Standard TextEncoder APIに準拠している。

公式ドキュメント

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2