I have byte array and I can convert this usin Convert.ToBase64String() method in c#. I wrote equivalent of this method in javascript like below. But the result is different.
in c#:
byte[] data = ...
Convert.ToBase64String(data)
in js
function GetStringFromByteArray(array) {
var result = "";
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++)
result += String.fromCharCode(array[i][j]);
}
return result;
}
How can I succeed this in js?
Yes, the result is different, because the Javascript function doesn't do base64 encoding at all.
The base64 encoded data contains six bits of information per character, so the eight bits of a character code is spread out over two characters in the encoded data.
To encode the data, you have to regroup the bits in the bytes into six bit groups, then you can convert each group into a base64 character.
See: Base64
You can use this javascript library
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With