js convert string to utf8

JavaScript
[Edit]
+
0
-
0

js convert string to utf8

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Note: JavaScript engine stores string literals in the UTF-16 encoding format. const utf16Text = 'I ❤️ JS'; // I ❤️ JS const utf8Text = unescape(encodeURIComponent(utf16Text)); // I ❤️ JS console.log(utf8Text); // I ❤️ JS // Warning: // // Although unescape() is not strictly deprecated (as in "removed from the Web standards"), // it is defined in Annex B of the ECMA-262 standard, whose introduction states: // // … All of the language features and behaviors specified in this annex have one or more // undesirable characteristics and in the absence of legacy usage would be removed from // this specification. … … Programmers should not use or assume the existence of these // features and behaviors when writing new ECMAScript code. … // // Source: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape // // See also: // https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD
[Edit]
+
0
-
0

js convert string to utf8

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Note: Almost all JavaScript engines by default store string literals using UTF-16 encoding format. // By using the below function we do conversion from default encoding to UTF-8 encoding. var toUtf8 = function(text) { var surrogate = encodeURIComponent(text); var result = ''; for (var i = 0; i < surrogate.length;) { var character = surrogate[i]; i += 1; if (character == '%') { var hex = surrogate.substring(i, i += 2); if (hex) { result += String.fromCharCode(parseInt(hex, 16)); } } else { result += character; } } return result; }; // Usage example: const utf16Text = 'I ❤️ JS'; // I ❤️ JS const utf8Text = toUtf8(utf16Text); // I ❤️ JS console.log(utf8Text); // I ❤️ JS // See also: // https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD
Reset
[Edit]
+
0
-
0

js convert string to utf8

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Note: JavaScript engine stores string literals in the UTF-16 encoding format. var toUtf8 = function(text) { return unescape(encodeURIComponent(text)); }; // Usage example: const utf16Text = 'I ❤️ JS'; // I ❤️ JS const utf8Text = toUtf8(utf16Text); // I ❤️ JS console.log(utf8Text); // I ❤️ JS // Warning: // // Although unescape() is not strictly deprecated (as in "removed from the Web standards"), // it is defined in Annex B of the ECMA-262 standard, whose introduction states: // // … All of the language features and behaviors specified in this annex have one or more // undesirable characteristics and in the absence of legacy usage would be removed from // this specification. … … Programmers should not use or assume the existence of these // features and behaviors when writing new ECMAScript code. … // // Source: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape // // See also: // https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD
[Edit]
+
0
-
0

js convert string to utf8

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Note: Almost all JavaScript engines by default store string literals using UTF-16 encoding format. // By using the below function we do conversion from default encoding to UTF-8 encoding. function toUtf8(text) { var result = ''; for (var i = 0; i < text.length; i++) { var code = text.charCodeAt(i); if (code < 0x80) { result += String.fromCharCode(code); } else { if (code < 0x800) { result += String.fromCharCode(code >> 6 & 0x1F | 0xC0); } else { result += String.fromCharCode(code >> 12 | 0xE0); result += String.fromCharCode(code >> 6 & 0x3F | 0x80); } result += String.fromCharCode(code & 0x3F | 0x80); } } return result; } // Usage example: const utf16Text = 'I ❤️ JS'; // I ❤️ JS const utf8Text = toUtf8(utf16Text); // I ❤️ JS console.log(utf8Text); // I ❤️ JS // See also: // https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD
Reset