window.ENTITIES={'/api/snippets/javascript/js%20convert%20string%20to%20utf8':[{"result":true,"message":null,"batch":{"type":"javascript","name":"js convert string to utf8","items":[{"id":"1yv3ND","type":"javascript","name":"js convert string to utf8","content":"// Note: JavaScript engine stores string literals in the UTF-16 encoding format.\n\nconst utf16Text = 'I ❤️ JS'; // I ❤️ JS\nconst utf8Text = unescape(encodeURIComponent(utf16Text)); // I ❤️ JS\n\nconsole.log(utf8Text); // I ❤️ JS\n\n\n// Warning:\n//\n// Although unescape() is not strictly deprecated (as in \"removed from the Web standards\"),\n// it is defined in Annex B of the ECMA-262 standard, whose introduction states:\n//\n// … All of the language features and behaviors specified in this annex have one or more\n// undesirable characteristics and in the absence of legacy usage would be removed from\n// this specification. … … Programmers should not use or assume the existence of these \n// features and behaviors when writing new ECMAScript code. …\n//\n// Source: \n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape\n//\n// See also:\n// https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD","source":"https://dirask.com/posts/JavaScript-base64-with-Unicode-support-D6BdmD","author":{"id":"eaBBla","name":"FryerTuck","avatar":"1629139808762__eaBBla__w40px_h40px.jpg","points":649,"role":"BASIC"},"creationTime":1636718818000,"updateTime":1636721008000,"removalTime":null},{"id":"jo5A4D","type":"javascript","name":"js convert string to utf8","content":"// ONLINE-RUNNER:browser;\n\n// Note: Almost all JavaScript engines by default store string literals using UTF-16 encoding format.\n// By using the below function we do conversion from default encoding to UTF-8 encoding.\n\nvar toUtf8 = function(text) {\n \tvar surrogate = encodeURIComponent(text);\n \tvar result = '';\n for (var i = 0; i < surrogate.length;) {\n var character = surrogate[i];\n\t\ti += 1;\n if (character == '%') {\n \tvar hex = surrogate.substring(i, i += 2);\n\t\t\tif (hex) {\n\t\t\t\tresult += String.fromCharCode(parseInt(hex, 16));\n\t\t\t}\n } else {\n \tresult += character;\n }\n }\n return result;\n};\n\n\n// Usage example:\n\nconst utf16Text = 'I ❤️ JS'; // I ❤️ JS\nconst utf8Text = toUtf8(utf16Text); // I ❤️ JS\n\nconsole.log(utf8Text); // I ❤️ JS\n\n\n// See also:\n// https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD","source":"https://dirask.com/posts/JavaScript-base64-with-Unicode-support-D6BdmD","author":{"id":"eaBElD","name":"Leen-Kerr","avatar":"1629131359518__eaBElD__w40px_h40px.jpg","points":571,"role":"BASIC"},"creationTime":1636718967000,"updateTime":1697172199000,"removalTime":null},{"id":"1GKe91","type":"javascript","name":"js convert string to utf8","content":"// Note: JavaScript engine stores string literals in the UTF-16 encoding format.\n\nvar toUtf8 = function(text) {\n \treturn unescape(encodeURIComponent(text));\n};\n\n\n// Usage example:\n\nconst utf16Text = 'I ❤️ JS'; // I ❤️ JS\nconst utf8Text = toUtf8(utf16Text); // I ❤️ JS\n\nconsole.log(utf8Text); // I ❤️ JS\n\n\n// Warning:\n//\n// Although unescape() is not strictly deprecated (as in \"removed from the Web standards\"),\n// it is defined in Annex B of the ECMA-262 standard, whose introduction states:\n//\n// … All of the language features and behaviors specified in this annex have one or more\n// undesirable characteristics and in the absence of legacy usage would be removed from\n// this specification. … … Programmers should not use or assume the existence of these \n// features and behaviors when writing new ECMAScript code. …\n//\n// Source:\n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape\n//\n// See also:\n// https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD","source":"https://dirask.com/posts/JavaScript-base64-with-Unicode-support-D6BdmD","author":{"id":"zDvpRo","name":"Vanessa-Drake","avatar":"1629058828093__zDvpRo__w40px_h40px.jpg","points":718,"role":"BASIC"},"creationTime":1636719181000,"updateTime":1636721036000,"removalTime":null},{"id":"pYPLwD","type":"javascript","name":"js convert string to utf8","content":"// ONLINE-RUNNER:browser;\n\n// Note: Almost all JavaScript engines by default store string literals using UTF-16 encoding format.\n// By using the below function we do conversion from default encoding to UTF-8 encoding.\n\nfunction toUtf8(text) {\n var result = '';\n for (var i = 0; i < text.length; i++) {\n var code = text.charCodeAt(i);\n if (code < 0x80) {\n result += String.fromCharCode(code);\n } else {\n if (code < 0x800) {\n result += String.fromCharCode(code >> 6 & 0x1F | 0xC0);\n } else {\n result += String.fromCharCode(code >> 12 | 0xE0);\n result += String.fromCharCode(code >> 6 & 0x3F | 0x80);\n }\n result += String.fromCharCode(code & 0x3F | 0x80);\n }\n }\n return result;\n}\n\n\n// Usage example:\n\nconst utf16Text = 'I ❤️ JS'; // I ❤️ JS\nconst utf8Text = toUtf8(utf16Text); // I ❤️ JS\n\nconsole.log(utf8Text); // I ❤️ JS\n\n\n// See also:\n// https://dirask.com/questions/What-encoding-uses-JavaScript-to-stores-string-jM73zD","source":"","author":{"id":"30Rr80","name":"Evania","avatar":"1629130333602__30Rr80__w40px_h40px.jpg","points":584,"role":"BASIC"},"creationTime":1713040798000,"updateTime":1713040822000,"removalTime":null}]}}]};