Read/Write binary file data to/from string

F

Frank Uray

Hi all

I have not much experiance in reading/writing binary of files.

I would like to read the binary data of a file into a string.
It would nice to have it as string because I need to store it.
After this I would like to create a new file from this binary string data.

Is this possible ??

Thanks and best regards
Frank Uray
 
A

Alberto Poblacion

Frank Uray said:
I would like to read the binary data of a file into a string.
It would nice to have it as string because I need to store it.
After this I would like to create a new file from this binary string data.

Is this possible ??

You will need to convert the binary data into something that can be
represented as text characters. One popular encoding is called "Base 64". In
..Net, you can use the method Convert.ToBase64String to convert an array of
bytes read from the file into a string encoded in base 64. The inverse
process is achieved by means of Convert.FromBase64String.

using System.IO;
....
byte[] theBytes = File.ReadAllBytes(myFile);
string str = Convert.ToBase64String(theBytes);
 
F

Frank Uray

Hi Alberto

Thanks a lot, this is exactly what I need !! :))

Regards
Frank Uray


Alberto Poblacion said:
Frank Uray said:
I would like to read the binary data of a file into a string.
It would nice to have it as string because I need to store it.
After this I would like to create a new file from this binary string data.

Is this possible ??

You will need to convert the binary data into something that can be
represented as text characters. One popular encoding is called "Base 64". In
..Net, you can use the method Convert.ToBase64String to convert an array of
bytes read from the file into a string encoded in base 64. The inverse
process is achieved by means of Convert.FromBase64String.

using System.IO;
....
byte[] theBytes = File.ReadAllBytes(myFile);
string str = Convert.ToBase64String(theBytes);

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top