1

On Server in WebApiController i have :

private Byte[] bytes=new Byte[21];

after filling it looks like:

bytes = new byte{127,253,159,127,253,223,127,253,255,127,252,63,0,1,192,127,252,255,127,253,191};

I know that this will be a string:

111111101011111111111001111111101011111111111011111111101011111111111111111111100011111111111100000000001000000000000011111111100011111111111111111111101011111111111101

When I'm on the client receives a response from the server array looks like:

f/2ff/3ff/3/f/w/AAHAf/z/f/2/

This is a base64 format.How can I convert this post to a string type as

111111101011111111111001111111101011111111111011111111101011111111111111111111100011111111111100000000001000000000000011111111100011111111111111111111101011111111111101

Please help me find a solution to this problem. Implementation on JS or AngularJS.

Stewie
  • 60,366
  • 20
  • 146
  • 113
Junik
  • 11
  • 3
  • 3
    so... you want to [base-64 decode it](http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript) and then [get the binary representation of each part](http://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript), and then concatenate them? – Marc Gravell Jul 11 '13 at 14:30
  • I'm half tempted to downvote because certainly you should be able to find the base64-decoding part very easily. – millimoose Jul 11 '13 at 14:31
  • I found the problem. [Take a look here "The Unicode Problem"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding#The_.22Unicode_Problem.22) – Junik Jul 11 '13 at 15:29

1 Answers1

0

This will convert your byte array in a string with binary representation

var bytes = new byte[] {
    127, 253, 159, 127, 253, 223, 127, 253, 255, 127, 252, 63,
    0, 1, 192, 127, 252, 255, 127, 253, 191 };

var output = bytes
    .Select(delegate(byte s)
        {
            int value = s;
            var str = string.Empty;

            for (var count = 0; count < 8; count++, value /= 2)
                str = (value % 2) + str;

            return str;
        })
    .Aggregate((ac, i) => ac + i);

Console.WriteLine(output);
Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81