const text = "ま";
// UTF-8 → バイト列
const encoder = new TextEncoder();
const bytes = encoder.encode(text);
// わざと文字化け(UTF-8 → Shift_JISとして解釈)
const broken = new TextDecoder("shift_jis").decode(bytes);
const fixedBytes = new TextEncoder("shift_jis").encode(broken);
const fixed = new TextDecoder("utf-8").decode(fixedBytes);
console.log("元:", text);
console.log("文字化け:", broken);
console.log("復元:", fixed);
To embed this project on your website, copy the following code and paste it into your website's HTML: