How to read a binary file into String?  
Author Message
Jessica Alba





PostPosted: Visual C# Language, How to read a binary file into String? Top

Please note my binary here does not mean the 1s and 0s, but those unprintable characters.

I tried to use File.ReadAllText() but found that the data has been changed. And if I use File.ReadAllBytes(), then I found that in order to convert Byte[] to String, I need to define an encoding. But the data is not a language text but actually some image data.

What is the best way to convert a byte array of unprintable character to String

Please help. I will grade your answer.

Thanks



Visual C#1  
 
 
Matthew Watson





PostPosted: Visual C# Language, How to read a binary file into String? Top

If it is not text, why do you need to convert it to a string Why not leave it as an array of bytes

 
 
James Curran





PostPosted: Visual C# Language, How to read a binary file into String? Top

You're probably best going with Convert.ToBase64String(...) & Convert.FromBase64String(...).

A Base64 string (also known as UUENCODING) is the manner inwhich binary file are encoded for email attachments. In it, three binary bytes are converted into four printable characters.



 
 
cl408e





PostPosted: Visual C# Language, How to read a binary file into String? Top

If it is not text, why do you need to convert it to a string Why not leave it as an array of bytes

Good question. The reason is that I need to manipulate the content by adding some text in front and at the back of the data.

So I assume I need to read it from file, convert it to String, then add my text to it before sending it to printer to print.

Please help.


 
 
cl408e





PostPosted: Visual C# Language, How to read a binary file into String? Top

thanks for all the replies.

i found a stupid to do it.

I read the content as byte[] by using File.ReadAllBytes()

Then convert each byte (typecast it to integer) to Hex.

Then do my adding of text to the context.

Then convert it back to byte[].

Good day.