What is best way to read binary file into string?

S

Saga

Hi all,

I am now building a routine that reads a small (no larger than 100 bytes)
binary file
and saves the contents in a string. The file can contain characters anywhere
in the
0 - 255 ASCII range. After looking at MSDN Lib. and on the 'net I found a
few
approaches to this problem. I selected one; the resulting code:

Dim strBuf As String = ""
Dim i As Int32

'Check to see if file exists.
If System.IO.File.Exists(strFile) Then

'File exists. Read it.
Dim fs As New System.IO.FileStream(strFile, System.IO.FileMode.Open)
Dim bytAr(fs.Length - 1) As Byte

'Read the file contents.
fs.Read(bytAr, 0, bytAr.Length)
fs.Close()
fs = Nothing

'Build string from byte array.
For i = 0 To bytAr.GetUpperBound(0)
strBuf = strBuf & Chr(bytAr(i))
Next

MessageBox.Show(strBuf)
End if

Is this the best way to do this? It seems that all the methods that I found
always read in the bytes into a byte array. Is there is a way to read the
binary file directly into a string variable? Thanks for the continued
assistance! Saga
 
T

Tom Shelton

Hi all,

I am now building a routine that reads a small (no larger than 100 bytes)
binary file
and saves the contents in a string. The file can contain characters anywhere
in the
0 - 255 ASCII range. After looking at MSDN Lib. and on the 'net I found a
few
approaches to this problem. I selected one; the resulting code:

Dim strBuf As String = ""
Dim i As Int32

'Check to see if file exists.
If System.IO.File.Exists(strFile) Then

'File exists. Read it.
Dim fs As New System.IO.FileStream(strFile, System.IO.FileMode.Open)
Dim bytAr(fs.Length - 1) As Byte

'Read the file contents.
fs.Read(bytAr, 0, bytAr.Length)
fs.Close()
fs = Nothing

'Build string from byte array.
For i = 0 To bytAr.GetUpperBound(0)
strBuf = strBuf & Chr(bytAr(i))
Next

MessageBox.Show(strBuf)
End if

Is this the best way to do this? It seems that all the methods that I found
always read in the bytes into a byte array. Is there is a way to read the
binary file directly into a string variable? Thanks for the continued
assistance! Saga

unless the data is string data, then I wouldn't use a string datatype. A
string in .net is at minimum two bytes per char. Binary data, should be in
byte array.

If it's char data, then a streamreader would probably be your best bet, or if
you actually need to convert a byte array of acii char data, then I would
probably use the System.Text.Encoding class:

Dim str As String = Encoding.Ascii.GetString (File.ReadAllBytes("myfile"))

Or something of that nature... Though.. You might have problems with chr(0),
so you might need to test that.
 
S

Saga

Tom Shelton said:
unless the data is string data, then I wouldn't use a string datatype. A
string in .net is at minimum two bytes per char. Binary data, should be
in
byte array.
Thanks for your reply. The note above is very interesting. At this point I
was somewhat stubborn in converting the byte array into a string because
I use that as a parameter to another routine that does further processing
on the string, but I see that in the end it converts each char in the string
to ascii, so perhaps it is best to look at it using your advice in that I
read
in the data into the byte array and then pass the array and not the string.
Thanks again! Saga
 
T

Tom Shelton

I had almost the same reply to you like Tom, until he wrote about ASCII
which is a 7 bit translation and you miss then the first bit.

You are of course right - ascii is only 7 bits. But, extended ascii is the
full 8-bits... I didn't read carefully, when the op mentioned 0 - 255. I
only saw him mention ascii :)

The op will want to use the proper encoding for the task of course. Though
I'm unclear if the data in the bytes are actual string data, or just binary
data...
 
S

Saga

Tom Shelton said:
You are of course right - ascii is only 7 bits. But, extended ascii is
the
full 8-bits... I didn't read carefully, when the op mentioned 0 - 255. I
only saw him mention ascii :)

The joys of terminology. While writing my original post I only had "ASCII",
but knowing that in the strict sense of the word (acronym?) it defined only
values from 0 to 127, I decided to add the "0-255" to refer to the entire
ASCII set. My apologies for not including "extended".

The procedure that I call expects a string, but it can be composed of
something like (of course, these chars are read from the file):

sExample = chr(34) & chr(90) & chr(150) & chr(4) & chr(220)

The data in the binary file can have chars anywhere in the range of 0 to
255. I changed my logic so that the byte array that I read from the file
is passed directly to the procedure for further processing. This is now
working perfectly! Input from all is appreciated! Thanks, Saga
 
H

Herfried K. Wagner [MVP]

Am 10.03.2010 19:02, schrieb Saga:
I am now building a routine that reads a small (no larger than 100 bytes)
binary file
and saves the contents in a string. The file can contain characters anywhere
in the
0 - 255 ASCII range. After looking at MSDN Lib. and on the 'net I found a
few
approaches to this problem. I selected one; the resulting code:

\\\
Imports System.IO
....
Dim Text As String = File.ReadAllText(<path>, <encoding>)
///

The encoding must be chosen depending on how the text is encodded when
writing it to the file. 'Encoding.Default' will give you the system's
Windows ANSI codepage, for example.
 
S

Saga

\\\
Imports System.IO
...
Dim Text As String = File.ReadAllText(<path>, <encoding>)
///

The encoding must be chosen depending on how the text is encodded when
writing it to the file. 'Encoding.Default' will give you the system's
Windows ANSI codepage, for example.
Thanks! Saga
 
G

Göran Andersson

Saga said:
Hi all,

I am now building a routine that reads a small (no larger than 100 bytes)
binary file
and saves the contents in a string. The file can contain characters anywhere
in the
0 - 255 ASCII range. After looking at MSDN Lib. and on the 'net I found a
few
approaches to this problem. I selected one; the resulting code:

Dim strBuf As String = ""
Dim i As Int32

'Check to see if file exists.
If System.IO.File.Exists(strFile) Then

'File exists. Read it.
Dim fs As New System.IO.FileStream(strFile, System.IO.FileMode.Open)
Dim bytAr(fs.Length - 1) As Byte

'Read the file contents.
fs.Read(bytAr, 0, bytAr.Length)

You are not using the Read method correctly. The method returns the
number of bytes that was read, which can be smaller than the number of
bytes requested. To do it correctly you have to loop until you get all
the bytes:

Dim offset As Integer = 0
Do
Dim len As Integer = fs.Read(bytAr, offset, bytAr.Length - offset)
offset += len
Loop Until offset = byteAr.Length or len = 0

The variable offset now contains the number of bytes that was read into
the array. Normally this is the same as the size of the array, but you
might want to check this to be on the safe side.

Unless you are stuck with framework 1.x, an alternative to read the file
into a byte array is to simply use the ReadAllBytes method:

Dim bytAr As Byte() = File.ReadAllBytes(strFile)
fs.Close()
fs = Nothing

'Build string from byte array.
For i = 0 To bytAr.GetUpperBound(0)
strBuf = strBuf & Chr(bytAr(i))
Next

Converting bytes to character codes it not a good way of decoding a
file. You are converting something that is not unicode into unicode
characters. This works for the range 32 to 127 where the characters are
the same in the commonly used character encodings, but for values
outside of that you don't get anything useful.
MessageBox.Show(strBuf)
End if

Is this the best way to do this? It seems that all the methods that I found
always read in the bytes into a byte array. Is there is a way to read the
binary file directly into a string variable? Thanks for the continued
assistance! Saga

If you know the encoding of the text, you can use the ReadAllText method
to read the file and decode it into a string:

Dim encoding As Encoding = Encoding.GetEncoding("iso-8859-1")
Dim strBuf As String = File.ReadAllText(strFile, encoding)
 

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