Decode Base64 with UCS-2 Encoding
I want to decode the Base64 result of MS-SQL Server in javascript, but I can't find a true solution. I know SQL Server uses UCS-2 encoding, and I have to use the same encoding to decode in javascript.
For example, for MwZEBicGRQY=
the encoded result must be سلام
.
Do you have any solution to decode that using javascript?
Solution 1:
You can first decode your base64 data into an Uint8Array, and then read the resulting data as UTF-16:
const base64_string = "MwZEBicGRQY";
// from https://stackoverflow.com/a/41106346/3702797
// may not be bullet-proof
const arr = Uint8Array.from(atob(base64_string), c => c.charCodeAt(0))
const decoded = new TextDecoder("UTF-16").decode(arr);
console.log(decoded);