Read/Write binary file data to/from string

  • Thread starter Thread starter Frank Uray
  • Start date Start date
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
 
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);
 
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);

.
 
Back
Top