8

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?

1
  • 1
    Not duplicate. The question is about byte array, not string. Commented May 15, 2013 at 10:55

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can use this javascript library

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.