(Similar questions were asked in StackOverflow, but not quite. The closest one is probably โ javascript, how to convert a unicode string to ascii โ, where itโs already a remark โit should be dup [licate].โ I read a few similar messages, but they they donโt answer my specific question. I looked at very good W3Schools and also Google Googled, but still couldnโt find the answer. Therefore, any hints here are very appreciated.)
I have an array of bytes passed to a JavaScript fragment. In JavaScript, data comes in a string. I do not know the transfer mechanism, as it comes from a third-party application. I donโt even know if the string is โwideโ or โnarrowโ.
In my JavaScript, I have code like b = str.charCodeAt(pos);.
My problem is that a byte value like 0x86 = 134 goes through the character 0x2020 = 8224. It looks like my source byte is interpreted as a Latin character (1) (possibly) and then translated into an equivalent code Unicode point. (The problem may or may not be a JavaScript error.) Similar problems occur with other values, although the ranges 0x00..0x7F and 0xA0..0xFF seem accurate, but most values โโfrom 0x80..0x9F are affected, in each case the value seems to be Unicode for source Latin-1.
Another observation is that the length of the string is what I would expect for a narrow string if the length were measured in bytes. (On the other hand, if length returns a value in abstract characters, this tells me nothing.)
So, in JavaScript, is there a way to get the raw bytes in a string, or get the Latin-1 or ASCII character code directly, or convert the character encoding, or determine the default encoding?
I could write my own mapping, but I would prefer. I expect that this is what I will eventually do, but it has a sense of breaking into shreds.
I am also exploring if there is anything that I can configure in the calling application (since it could pass data as a wide string, although I doubt it).
However, I would be wondering if there is a simple JavaScript solution or understand why this is not happening.(If the input was character data, using Unicode with that would automatically be great, but it's not just a stream of binary data.)
Thank.