read binary file to a byte array

  • Thread starter Thread starter Siva
  • Start date Start date
Siva said:
thanks and how do convert this byte[] to string?

First you need to know what kind of character set is used to encode the
string in those bytes. Once you know it, you use the GetString method in
System.Text.Encoding to perform the conversion:

string s = System.text.Encoding.UTF8.GetString(data);

The preceding presumes that the file was encoded in UTF8, but if another
encoding was used you need to use the proper encoding. For instance, if the
file used the Windows-1252 character set, you could use this:

string s = System.text.Encoding.GetEncoding(1252).GetString(data);
 
Siva said:
thanks and how do convert this byte[] to string?

I thought you said the file had binary data... :)

You could perhaps use this instead:

string txt = File.ReadAllText("somefile.bin");

Mike
 
hello,
Sorry for the confusion, here is the code,

byte[] data = File.ReadAllBytes(<filepath>);
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(data);

now i need to convert this 'hash' to string with hexadecimal representation.
is a ready made method available already?

Siva

Kerem Gümrükcü said:
Hi Siva,
how would i read a binary file into a byte array efficiently?

this is the fastest way to do it in one shot:

byte[] yourFileBytes = File.ReadAllBytes(@"C:\Path\To\Your\File.txt");

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
 
Siva said:
hello,
Sorry for the confusion, here is the code,

byte[] data = File.ReadAllBytes(<filepath>);
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(data);

now i need to convert this 'hash' to string with hexadecimal representation.
is a ready made method available already?

Siva

Siva,

Depending on the exact format you want, you could do something like:

foreach (byte b in hash)
Console.WriteLine(b.ToString("x"));

Mike
 
Siva said:
Sorry for the confusion, here is the code,

byte[] data = File.ReadAllBytes(<filepath>);
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(data);

now i need to convert this 'hash' to string with hexadecimal
representation.
is a ready made method available already?

Does it need to be hexadecimal? There is a more compact representation which
is called "Base 64". This instruction produces a base 64 string from your
byte array:

string s = Convert.ToBase64String(hash);
 
Depending on the exact format you want, you could do something like:

foreach (byte b in hash)
Console.WriteLine(b.ToString("x"));

I'd amend that to

Console.WriteLine(b.ToString("x:2"))

So that all bytes will be 2 characters, or those pesky less-than-16 bytes
will screw up your string.
 
I'd amend that to

Console.WriteLine(b.ToString("x:2"))

So that all bytes will be 2 characters, or those pesky less-than-16 bytes
will screw up your string.

Oh, you just took all of the fun out of parsing the result string! :)
 
I'd amend that to

Console.WriteLine(b.ToString("x:2"))

For posterity I'll admend that a second time and remove the colon that I
typed for no good reason:

Console.WriteLine(b.ToString("x2"))
 
Back
Top