.NET Framework - read binary file to a byte array

Asked By Siva
25-Feb-10 06:41 AM
how would i read a binary file into a byte array efficiently?
ToBase64String
(1)
ReadAllBytes
(1)
ReadAllText
(1)
ComputeHash
(1)
GetEncoding
(1)
GetString
(1)
Encoding
(1)
Console
(1)
  Kerem Gümrükcü replied to Siva
25-Feb-10 06:54 AM
Hi Siva,


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
-----------------------
  Family Tree Mike replied to Siva
25-Feb-10 07:16 AM
byte [] data = File.ReadAllBytes("Somefilename.bin");

Mike
  Siva replied to Family Tree Mike
25-Feb-10 08:09 AM
thanks and how do convert this byte[] to string?
  Alberto Poblacion replied to Siva
25-Feb-10 08:19 AM
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);
  rossum replied to Siva
25-Feb-10 09:20 AM
Why not just read the file as a text file directly into a string?  Is
there a reason that you need the same data twice, once as bytes and
once as a string?

rossum
  Family Tree Mike replied to Siva
25-Feb-10 09:50 AM
I thought you said the file had binary data... :)

You could perhaps use this instead:

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

Mike
  Siva replied to Kerem Gümrükcü
26-Feb-10 06:58 AM
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
  Family Tree Mike replied to Siva
26-Feb-10 07:12 AM
Siva,

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

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

Mike
  Alberto Poblacion replied to Siva
26-Feb-10 08:27 AM
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);
  Jeff Johnson replied to Family Tree Mike
26-Feb-10 10:36 AM
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.
  Family Tree Mike replied to Jeff Johnson
26-Feb-10 05:04 PM
Oh, you just took all of the fun out of parsing the result string! :)

--
Mike
  Jeff Johnson replied to Jeff Johnson
12-Mar-10 03:55 PM
For posterity I will admend that a second time and remove the colon that I
typed for no good reason:

Console.WriteLine(b.ToString("x2"))
Create New Account
help
a couple of instances of the array inside the file. You can just use File.ReadAllText() to get the contents, pass it line and line and load it up. I am with test data using delphi (3 or 4 records), then from C#: Console.WriteLine(Convert.ToBase64String(File.ReadAllBytes("filename.ext"))); And post back the output. That will survive being sent as plaint text
File.ReadAllText prevent write access for other processes .NET Framework Hi, I have noticed that File.ReadAllText prevents other processes from writing to the file while reading. It seems like the FileShare rather than ReadWrite. This is a bug in my opinion. / m .NET Framework Discussions File.ReadAllText (1) ReadAllText (1) FileStream (1) FileShare (1) Marthin (1) Freij (1) Shure (1) Enum (1) Hello, Marthin 10 Oct 2007 04:44:01 -0700: MF> Hi, MF> I have noticed that File.ReadAllText prevents other processes from MF> writing to the file while reading. It seems like the use the File class. It is intended only for the basic scenarios. Pete keywords: File.ReadAllText, prevent, write, access, for, other, processes description: Hi, I have noticed that File.ReadAllText prevents other processes from writing to the file while reading. It seems like the FileShare
page to my application and display it in a webbrowser control ? Robertico C# Discussions File.ReadAllText (1) ReadAllText (1) WebBrowser1.DocumentText (1) Copy (1) Mike (1) Webbrowser (1) Yes. Add an html page to "Copy if newer". Then in your form app, use a line: webBrowser1.DocumentText = File.ReadAllText("somefile.htm"); Mike how do I add the html page to my project? Robertico Received