Reputation: 1396
How to decode cp-1251
to UTF-8
in javascript?
The cp-1251
is from a datafeed, which required to decode from js client side.
There is no way to change server side output, since it is related to a 3rd party, and due to some reason, I would not use any server side programming to convert the datafeed to become another datafeed.
Upvotes: 3
Views: 7446
Reputation: 32063
(Assuming that by "UTF-8" you meant the JS strings in their native encoding...)
Depending on the format your 'cp-1251' data is in and depending on the browsers you need to support, you can choose from:
Uint8Array
) - if you're using web sockets, you can get an ArrayBuffer out of it to decode.String
s consisting of characters like \u00XY
, where 0xXY is the encoded byte.Note that in most cases (not something as low-level as websockets though) it might be easier to read the data in the correct encoding before it ends up as a JS string (for example, you can force XMLHttpRequest to use a certain encoding even if the server misreports the encoding).
Upvotes: 2