Problem with byte array in C #

I found this code snippet on a blog as "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 the output of AAAAAQAB ..

What is unclear is how 000101 maps to AAAAAQAB , and can I use this for all az characters as the binary equivalent and how? or is there any other method?

+6
source share
2 answers

In fact, 00000000 00000000 00000000 00000001 00000000 00000001 mapped to AAAAAQAB because base64 uses 6 bits per letter, therefore:

 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.

+5
source

The ToBase64String method ToBase64String as follows. ( from the wiki )

Base64 is a group of similar coding schemes that represent binary data in ASCII string format, translating it into a radix-64 representation. The term Base64 comes from a specific encoding of the transmission of MIME content.

To use string as byte[] or otherwise, you can use Encoding

 Encoding.UTF8.GetString(bytes); 

So,

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

equally

 Hello World 

To and from bytes:

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

Source: https://habr.com/ru/post/888086/


All Articles