javascript - Create invalid UTF8 string -
is possible create invalid utf8 string using javascript?
every solution i've found relies string.fromcharcode
generates undefined
rather invalid string. i've seen mention of errors being generated ill-formed utf8 string (i.e. https://developer.mozilla.org/en-us/docs/web/api/websocket#send()) can't figure out how create one.
a string in javascript counted sequence of utf-16 code units. there implicit contract code units represent unicode codepoints. so, possible represent sequence of utf-16 code units—even unpaired surrogates.
i find string.fromcharcode(0xd801)
returns replacement character, seems quite reasonable (rather undefined
). text function might but, efficiency reasons, i'm sure many text manipulations pass invalid sequences through unless manipulation required interpreting them codepoints.
the easiest way create such string string literal. example, "\ud83d \udeb2"
or "\ud83d"
or "\udeb2"
instead of valid "\ud83d\udeb2"
.
"\ud83d \udeb2".replace(" ","")
return "\ud83d\udeb2"
("🚲"
) don't think should count on coming string isn't valid utf-16 encoding of unicode codepoints.
Comments
Post a Comment