I'm writing a client-side code in JS , which should be compatible with a third-party c# service which I cannot modify at all.
The service supplies a base64 unicode encoded string (a JSON), for the sake of the (simplified) example this is the c# code:
var myString = Encoding.Unicode.GetBytes("{\"counter\":0}");
var encodedString = Convert.ToBase64String(myString);
which results in : "ewAiAGMAbwB1AG4AdABlAHIAIgA6ADAAfQA="
now I need to update this encoded JSON at the client-side, and return a valid base64 string which will be decoded by this C# code:
byte[] bytes = Convert.FromBase64String(encodedString);
string decodedString = Encoding.Unicode.GetString(bytes);
All the solutions/examples I've found online resulted in the decoding functions to output a invalid result.
for example, when using the basic window.btoa("{\"counter\":1}") resulted in eyJjb3VudGVyIjoxfQ==
which in turn , when decoding on the c# app, will be cause a format exception at best, or result in total gibberish.
any Ideas?
window.btoait uses UTF8 encoding, when you try to decode it as a Unicode string, it will ofcourse end up as gibberish. The C# conversion code is fun, but if you don't supply it Unicode input, it will ofcourse crash when doingEncoding.Unicode.GetString. What happens when you changeUnicodetoUTF8in the C# code?Encoding.UTF8then the decoding works fine. But if I change it toEncoding.UTF16it crashed. The problem is I have no control over the C# code, I need to figure it out on the JS side –