Reputation: 321
I have a string that represents some binary data, looking like:
\x89PNG\x1a\x00\x00\x00IHDR\x00\x00
etc
I need to post this string to some API, etc. AS IS but the problem is that Javascript automatically converts it to
PNG
etc
.escape, .encodeURI.. etc don't help
In Python such conversion can be done like string.encode('UTF-8') but I can't find nothing like that in JS.
Maybe someone knows the library or something that may help?
Upvotes: 6
Views: 2037
Reputation: 671
In Javascript we usualy use Base64 for this.
You can do something like
var encodedData = window.btoa(stringToEncode);
var decodedData = window.atob(encodedData);
You may also find this interesting
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}
function decode_utf8(s) {
return decodeURIComponent(escape(s));
}
Or reference https://stackoverflow.com/a/22373061/6302200
Upvotes: 1