Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

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

$
0
0

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

conststr='Hello こんにちは'constencoder=newTextEncoder()constencoded=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に準拠している。

公式ドキュメント


Viewing all articles
Browse latest Browse all 8691

Trending Articles