6

I found this code snippet on a blog as a "Convert Binary data to text"

Byte[] arrByte = {0,0,0,1,0,1};

string x = Convert.ToBase64String(arrByte);
System: Console.WriteLine(x);

And this provides a output of AAAAAQAB ..

What is not clear is that how 000101 -> is mapped to AAAAAQAB, and will I able to use this to all a-z characters as a binary equivalent and how? or is there a any other method ?

1
  • Could you clarify your question? The base 64 algorithm is easy to find (and equally easy to undestand in my opinion). See e.g. en.wikipedia.org/wiki/Base64. Commented May 14, 2011 at 3:22

2 Answers 2

5

Actually 00000000 00000000 00000000 00000001 00000000 00000001 is mapped to AAAAAQAB because base64 uses 6 bits per letter so:

000000 = A (0)
000000 = A
000000 = A
000000 = A
000000 = A
010000 = Q  (16)
000000 = A
000001 = B  (1)

See this Wikipedia article for more details.

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

5 Comments

+1 Nicey illustrates what the problem the OP had. That an array of bytes is not an array of bits
hi thanks for the answer my other problem is how other characters can be mapped in here ? thats the thing i dont get :-)
@sudantha I don't understand your second question. Can you explain?
Richard if A is 000000 (0) what C will be ? what will Z will be lyc that ?
Did you take a peek at that Wikipedia article? It has a very nicely laid out table that shows which value goes with which letter, uppercase and lowercase. C=3 and Z=25.
2

The method you are using, ToBase64String is the following. (from wiki)

Base64 is a group of similar encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The Base64 term originates from a specific MIME content transfer encoding.

To use a string as a byte[] or the other way you can use Encoding

Encoding.UTF8.GetString(bytes);

So

72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100

is equals to

Hello World

To bytes and from bytes:

var bytes = Encoding.UTF8.GetBytes("Hello world");
var str   = Encoding.UTF8.GetString(bytes);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.