Some other answers here have suggested working around this with decodeURIComponent and encodeURIComponent, this has proven to be unreliable and unpredictable. the second one is to convert the UTF-16 DOMString to an UTF-8 array of characters and then encode it.Ī note on previous solutions: the MDN article originally suggested using unescape and escape to solve the Character Out Of Range exception problem, but they have since been deprecated.the first one is to escape the whole string (with UTF-8, see encodeURIComponent) and then encode it.There are two possible methods to solve this problem: Though the original MDN article no longer exists, this solution is still arguably a better one, and does a great job of solving “The Unicode Problem” while maintaining plain text base64 strings that you can decode on, say,. The entire history of this question shows just how many different ways we’ve had to work around broken encoding systems over the years. Solution with ASCII base64 interoperability If, however, you want to preserve the UTF-8 functionality, you’re better off using the solution described below. If this doesn’t matter to you (i.e., you aren’t converting strings represented in UTF-8 from another system), then you’re good to go. This is because it is a binary encoded string, not a UTF-8 encoded string. Where this fails a little, is that you’ll notice the encoded string EycgAOAAIABsAGEAIABtAG8AZABlAA= no longer matches the previous solution’s string 4pyTIMOgIGxhIG1vZGU=. Let decoded = fromBinary(encoded) // "✓ à la mode" Return omCharCode(.new Uint16Array(bytes.buffer)) The solution recommended by MDN is to actually encode to and from a binary string representation: Encoding UTF8 ⇢ binaryĬonst bytes = new Uint8Array(binary.length) (Keep scrolling for the ASCII base64 solution) Since DOMStrings are 16-bit-encoded strings, in most browsers calling window.btoa on a Unicode string will cause a Character Out Of Range exception if a character exceeds the range of a 8-bit byte (0x00~0xFF). The original, now-dead MDN article explained: atob, which have since been mended in modern ECMAScript. The original MDN article also covered the broken nature of window.btoa and. So if you pass a string into btoa() containing characters that occupy more than one byte, you will get an error, because this is not considered binary data. In terms of JavaScript strings, this means strings in which each character occupies only one byte. Console.log(ok.codePointAt(0).toString(16)) // 61: occupies 1 byteīase64, by design, expects binary data as its input.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |