To answer the original question: here is how you decode utf-8 in javascript:
http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html
Specifically,
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}
function decode_utf8(s) {
return decodeURIComponent(escape(s));
}
We have been using this in our production code for 6 years, and it has worked flawlessly.
Note, however, that escape() and unescape() are deprecated. See this.
Answer from CpnCrunch on Stack Overflow Top answer 1 of 16
192
To answer the original question: here is how you decode utf-8 in javascript:
http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html
Specifically,
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}
function decode_utf8(s) {
return decodeURIComponent(escape(s));
}
We have been using this in our production code for 6 years, and it has worked flawlessly.
Note, however, that escape() and unescape() are deprecated. See this.
2 of 16
36
This should work:
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
Check out the JSFiddle demo.
Also see the related questions: here and here
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › TextDecoder
TextDecoder - Web APIs | MDN
June 28, 2025 - const utf8decoder = new TextDecoder(); // default 'utf-8' const encodedText = new Uint8Array([240, 160, 174, 183]); const output = document.querySelector("#output"); const decodeButton = document.querySelector("#decode"); decodeButton.addEventListener("click", () => { output.textContent = utf8decoder.decode(encodedText); }); const resetButton = document.querySelector("#reset"); resetButton.addEventListener("click", () => { window.location.reload(); });
GitHub
github.com › mathiasbynens › utf8.js
GitHub - mathiasbynens/utf8.js: A robust JavaScript implementation of a UTF-8 encoder/decoder, as defined by the Encoding Standard. · GitHub
utf8.decode('\xC2\xA9'); // → '\xA9' utf8.decode('\xF0\x90\x80\x81'); // → '\uD800\uDC01' // → U+10001 LINEAR B SYLLABLE B038 E · A string representing the semantic version number. utf8.js has been tested in at least Chrome 27-39, Firefox 3-34, Safari 4-8, Opera 10-28, IE 6-11, Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.11, PhantomJS 1.9.0, and Rhino 1.7RC4.
Author mathiasbynens
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
CodePen
codepen.io › piotezaza › pen › BvvMRm
Encode VS Decode UTF8 JavaScript
// ----- FUNCTIONS ----- // // ENCODE UTF8 function encode_utf8(s) { return unescape(encodeURIComponent(s)); } // DECODE UTF8 function decode_utf8(s) { return decodeURIComponent(escape(s)); } // ----- ENCODE & DECODE ----- // // ENCODE let from_1 = 'éàçè'; let result_1 = encode_utf8(from_1); $('.encode').html(result_1) // DECODE let from_2 = 'éà çè'; let result_2 = decode_utf8(from_2); $('.decode').html(result_2)
Blogger
ecmanaut.blogspot.com › 2006 › 07 › encoding-decoding-utf8-in-javascript.html
ecmanaut: Encoding / decoding UTF8 in javascript
From time to time it has somewhat ... I realized today: function encode_utf8(s) { return unescape(encodeURIComponent(s)); } function decode_utf8(s) { return decodeURIComponent(escape(s)); } 2012 Update: Monsur Hossain took a moment to explain how and why this works....
npm
npmjs.com › package › utf8
utf8 - npm
December 4, 2017 - (If you need to be able to encode ... see http://codepoints.net/U+00A9 ... Decodes any given UTF-8-encoded string (byteString) as UTF-8, and returns the UTF-8-decoded version of the string....
» npm install utf8
Published Dec 04, 2017
Version 3.0.0
Mother Eff
mothereff.in › utf-8
UTF-8 encoder/decoder
This tool uses utf8.js to UTF-8-encode any string you enter in the ‘decoded’ field, or to decode any UTF-8-encoded string you enter in the ‘encoded’ field.
JSFiddle
jsfiddle.net › onigetoc › QmT59
javascript utf8 encode-decode - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
GitHub
gist.github.com › chrisveness › bcb00eb717e6382c5608
Utf8 string encode/decode using regular expressions · GitHub
str="\\320\\223"+String.fromCodePoint(0x1F60E)+"..."; Utf8Decode(Utf8Encode(str).replace(/\\[0-9][0-9][0-9]/g,function(s){return String.fromCharCode(parseInt(s.substr(1),8));}))
Webtoolkit
webtoolkit.info › javascript_utf8.html
Javascript UTF-8 - Javascript tutorial with example source code
November 17, 2013 - The encoding known today as UTF-8 was invented by Ken Thompson. UTF-8 is a variable-length character encoding for Unicode. It can represent any character in the Unicode standard, yet is backwards compatible with ASCII. Use this Javascript to encode decode UTF-8 data.
GitHub
github.com › Benzinga › utf8js
GitHub - Benzinga/utf8js: Fast UTF-8 encoding/decoding for browsers and Node.
utf8.js is a fast UTF-8 encoder/decoder for JavaScript. It attempts to use native encoding/decoding methods whenever possible. It requires Promises and Typed Arrays, both of which can be polyfilled.
Author Benzinga
Node.js
nodejs.org › api › string_decoder.html
String decoder | Node.js v26.5.0 Documentation
import { StringDecoder } from 'node:string_decoder'; import { Buffer } from 'node:buffer'; const decoder = new StringDecoder('utf8'); const cent = Buffer.from([0xC2, 0xA2]); console.log(decoder.write(cent)); // Prints: ¢ const euro = Buffer.from([0xE2, 0x82, 0xAC]); console.log(decoder.write(euro)); // Prints: € const { StringDecoder } = require('node:string_decoder'); const decoder = new StringDecoder('utf8'); const cent = Buffer.from([0xC2, 0xA2]); console.log(decoder.write(cent)); // Prints: ¢ const euro = Buffer.from([0xE2, 0x82, 0xAC]); console.log(decoder.write(euro)); // Prints: €
GitHub
gist.github.com › tudisco › 309475
javascript utf8 encode and decode · GitHub
The result should be F0 9D 8C 86. // …but with the current implementation: UTF8.encode('\uD834\uDF06') == '\xF0\x9D\x8C\x86' // false ... Decoder with full Unicode support: https://gist.github.com/pascaldekloe/62546103a1576803dade9269ccf76330