简体   繁体   中英

Create invalid UTF8 string

Is it possible to create an invalid UTF8 string using Javascript?

Every solution I've found relies String.fromCharCode which generates undefined rather than an invalid string. I've seen mention of errors being generated by ill-formed UTF8 string (ie https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#send() ) but I can't figure out how you would actually create one.

A string in JavaScript is a counted sequence of UTF-16 code units. There is an implicit contract that the code units represent Unicode codepoints. Even so, it is possible to represent any sequence of UTF-16 code units—even unpaired surrogates.

I find String.fromCharCode(0xd801) returns the replacement character, which seems quite reasonable (rather than undefined ). Any text function might do that but, for efficiency reasons, I'm sure that many text manipulations would just pass invalid sequences through unless the manipulation required interpreting them as codepoints.

The easiest way to create such a string is with a string literal. For example, "\? \?" or "\?" or "\?" instead of the valid "\?\?" .

"\? \?".replace(" ","") actually does return "\?\?" ( "🚲" ) but I don't think you should count on anything good coming from a string that isn't a valid UTF-16 encoding of Unicode codepoints.

One way to generate an invalid UTF-8 string with JavaScript is to take an emoji and remove the last byte.

For example, this will be an invalid UTF-8 string:

const invalidUtf8 = '🐶🐶🐶'.substr(0,5);

According to this answer ,

UTF-8 is an encoding of Unicode which can represent pretty-much every single character and glyph that has ever existed in recorded human history, so that extent there are no "invalid" UTF-8 characters.

So no , it is not possible to create invalid UTF-8 characters. Every single character is valid UTF-8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM