Convert binary to string and vice versa

I created a web service that returns a (binary) file. Unfortunately, I cannot use byte [], so I need to convert an array of bytes to a string. What I'm doing at the moment is the following (but this does not work):

Convert file to string:

byte[] arr = File.ReadAllBytes(fileName);
System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();  
string fileAsString = enc.GetString(arr);  

To check if this works correctly, I convert it via:

System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
byte[] file = enc.GetBytes(fileAsString);

But at the end, the original byte array and the byte array created from the string are not equal. Should I use a different method to read the file into an array of bytes?

+3
source share
1 answer

Use Convert.ToBase64Stringto convert it to text, and Convert.FromBase64Stringto convert back.

Encoding . - ... Encoding . , "" (, ISO Latin 1), , , ..

Base64 , ASCII, .

+8

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


All Articles