Skip to content

Encodings in JavaScript #263

Answered by matklad
bpj asked this question in Q&A
Dec 19, 2023 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

You can than subsequently encode the resulting string into whatever encoding you need using the TextEncoder API:

const encoder = new TextEncoder();
const view = encoder.encode("€");
console.log(view); // Uint8Array(3) [226, 130, 172]

https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API

Though it feels like an XY problem perhaps? In JS, you are expected to just use strings and mostly not care about their encoding.

Perhaps this will help: in Lua, like in C or Zig or Python2, strings are just sequence of bytes. Lua as a language is encoding-agnostic, but you, as a programmer, had to care about not confusing encodings fairly often.

In JavaScript, strings are fairly opaque objects, a…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@bpj
Comment options

@matklad
Comment options

Answer selected by bpj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants