Using ByteArrays

A

Anil Gupte

I am learning how to read and write files. Text files were easy, but binary
files have me confused. As per the tutorial, I used BinaryReader to read a
file and put the contents into a ByteArray:

Dim fsReadStream As New FileStream(MyFileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)

' This is the part where I don't know how to do

End While
brReader.Close()

How do I actually use the data in that ByteArray. I have basically created
a file containing some text, some images and some video (I plan to encrypt
this later). Now I want to open the file and read it, then parse it, put
the text into a label, put the image into a picturebox etc. Any tutorials
you can point me to, or any information at all will be useful.

Thanx,
 
O

Olie

It all depends on what type of data you want to read from the Binary
file and the type of that data. The example you have quoted simply
reads a single byte at a time out of the binary file which is not very
usful and very inefficient if you want to read a whole string or image.

I would recomend you look at some of the functions of BinaryReader as
it is designed to perform the file parsing and can realy help you. I
have to guess here as I do not know the format of your file but here is
an example:

UINT16 DataType
UINT32 DataSize
byte[] Data

With this structure of data in the binary file I would call these
functions on the BinaryReader object:

DataType = brReader.ReadUInt16()
DataSize = brReader.ReadUInt32()

and assuming the data is a string

string = brReader.ReadString()

or

MyByteArray = brReader.ReadBytes(DataSize)
string = MyByteArray.ToString()

You would obviously need to put in some checking to see when the end of
the file has been reached.
 
A

Anil Gupte

Olie:

I tried what you suggested and the string = brReader.ReadString() worked
fine for the first part of the file, because it was string data and I put it
in a loop as below:

While brReader.PeekChar() > 31 And brReader.PeekChar() < 127
strRead = strRead & brReader.ReadString()
Label1.Text = strRead
End While
However, the rest is not string data and I don't know how to treat it. As I
mentioned in my original message, I am trying to create a composite file
(like a package) consisting of text, pictures and other types of data (even
a video). What I would ideally like is something like this:

While brReader.PeekChar() > -1 ' until EOF
Read some data
Figure out what is in it (Text, part of an image. whatever)
Take the text part and put it in a label to display (as above)
Store the binary part in a variable (or append if I already have some in
there)
... and keep going in this loop till I reach the end of the binary part
Put the binary part in a PictureBox (assuming for the moment it is an
image)
Back to the top of the loop to read some more data (could be more text
or another picture or whatever)
End While

Of course, I am the creator of the file (I wrote another program to write
these types of files), so I can put markers that tell me where the text ends
and the image part starts etc.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Olie said:
It all depends on what type of data you want to read from the Binary
file and the type of that data. The example you have quoted simply
reads a single byte at a time out of the binary file which is not very
usful and very inefficient if you want to read a whole string or image.

I would recomend you look at some of the functions of BinaryReader as
it is designed to perform the file parsing and can realy help you. I
have to guess here as I do not know the format of your file but here is
an example:

UINT16 DataType
UINT32 DataSize
byte[] Data

With this structure of data in the binary file I would call these
functions on the BinaryReader object:

DataType = brReader.ReadUInt16()
DataSize = brReader.ReadUInt32()

and assuming the data is a string

string = brReader.ReadString()

or

MyByteArray = brReader.ReadBytes(DataSize)
string = MyByteArray.ToString()

You would obviously need to put in some checking to see when the end of
the file has been reached.
 
A

Anil Gupte

BTW, I forgot to mention:
MyByteArray = brReader.ReadBytes(DataSize)
string = MyByteArray.ToString()

does not work, because apparently there is no method called ToString for
MyByteArray

--
Anil Gupte
www.keeninc.net
www.icinema.com

Olie said:
It all depends on what type of data you want to read from the Binary
file and the type of that data. The example you have quoted simply
reads a single byte at a time out of the binary file which is not very
usful and very inefficient if you want to read a whole string or image.

I would recomend you look at some of the functions of BinaryReader as
it is designed to perform the file parsing and can realy help you. I
have to guess here as I do not know the format of your file but here is
an example:

UINT16 DataType
UINT32 DataSize
byte[] Data

With this structure of data in the binary file I would call these
functions on the BinaryReader object:

DataType = brReader.ReadUInt16()
DataSize = brReader.ReadUInt32()

and assuming the data is a string

string = brReader.ReadString()

or

MyByteArray = brReader.ReadBytes(DataSize)
string = MyByteArray.ToString()

You would obviously need to put in some checking to see when the end of
the file has been reached.
 
O

Olie

I explained in my previous post how to read a single UInt value out of
the binary file. Just simply read out a value first to deternmine what
type of data follows (string, picture, video). How you go about reading
out the data depends on what variable you want to read the data to. In
the case of a picture, the Bitmap constructor takes in a stream
directly so you could just call the bitmap constructor with
fsReadStream.

Picture = New Bitmap(fsReadStream)

I was guessing that there was a ToString() function on a byte array.
The correct code to convert a byte array to a string is:

Dim Encoder As New System.Text.UTF7Encoding()
Dim MyString As String = Encoder.GetString(ByteArray)

As for the video, I am not sure what to do with that and I think that
reading a whole video into RAM is probably not the best idea. You could
extract the video section to a seperate file and then just play the
video from there.

Anil said:
Olie:

I tried what you suggested and the string = brReader.ReadString() worked
fine for the first part of the file, because it was string data and I put it
in a loop as below:

While brReader.PeekChar() > 31 And brReader.PeekChar() < 127
strRead = strRead & brReader.ReadString()
Label1.Text = strRead
End While
However, the rest is not string data and I don't know how to treat it. As I
mentioned in my original message, I am trying to create a composite file
(like a package) consisting of text, pictures and other types of data (even
a video). What I would ideally like is something like this:

While brReader.PeekChar() > -1 ' until EOF
Read some data
Figure out what is in it (Text, part of an image. whatever)
Take the text part and put it in a label to display (as above)
Store the binary part in a variable (or append if I already have some in
there)
... and keep going in this loop till I reach the end of the binary part
Put the binary part in a PictureBox (assuming for the moment it is an
image)
Back to the top of the loop to read some more data (could be more text
or another picture or whatever)
End While

Of course, I am the creator of the file (I wrote another program to write
these types of files), so I can put markers that tell me where the text ends
and the image part starts etc.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Olie said:
It all depends on what type of data you want to read from the Binary
file and the type of that data. The example you have quoted simply
reads a single byte at a time out of the binary file which is not very
usful and very inefficient if you want to read a whole string or image.

I would recomend you look at some of the functions of BinaryReader as
it is designed to perform the file parsing and can realy help you. I
have to guess here as I do not know the format of your file but here is
an example:

UINT16 DataType
UINT32 DataSize
byte[] Data

With this structure of data in the binary file I would call these
functions on the BinaryReader object:

DataType = brReader.ReadUInt16()
DataSize = brReader.ReadUInt32()

and assuming the data is a string

string = brReader.ReadString()

or

MyByteArray = brReader.ReadBytes(DataSize)
string = MyByteArray.ToString()

You would obviously need to put in some checking to see when the end of
the file has been reached.


Anil said:
I am learning how to read and write files. Text files were easy, but
binary
files have me confused. As per the tutorial, I used BinaryReader to read
a
file and put the contents into a ByteArray:

Dim fsReadStream As New FileStream(MyFileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)

' This is the part where I don't know how to do

End While
brReader.Close()

How do I actually use the data in that ByteArray. I have basically
created
a file containing some text, some images and some video (I plan to
encrypt
this later). Now I want to open the file and read it, then parse it, put
the text into a label, put the image into a picturebox etc. Any
tutorials
you can point me to, or any information at all will be useful.

Thanx,
 
A

Anil Gupte

Thanx much, I will try it. Appreciate the help.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Olie said:
I explained in my previous post how to read a single UInt value out of
the binary file. Just simply read out a value first to deternmine what
type of data follows (string, picture, video). How you go about reading
out the data depends on what variable you want to read the data to. In
the case of a picture, the Bitmap constructor takes in a stream
directly so you could just call the bitmap constructor with
fsReadStream.

Picture = New Bitmap(fsReadStream)

I was guessing that there was a ToString() function on a byte array.
The correct code to convert a byte array to a string is:

Dim Encoder As New System.Text.UTF7Encoding()
Dim MyString As String = Encoder.GetString(ByteArray)

As for the video, I am not sure what to do with that and I think that
reading a whole video into RAM is probably not the best idea. You could
extract the video section to a seperate file and then just play the
video from there.

Anil said:
Olie:

I tried what you suggested and the string = brReader.ReadString() worked
fine for the first part of the file, because it was string data and I put
it
in a loop as below:

While brReader.PeekChar() > 31 And brReader.PeekChar() < 127
strRead = strRead & brReader.ReadString()
Label1.Text = strRead
End While
However, the rest is not string data and I don't know how to treat it.
As I
mentioned in my original message, I am trying to create a composite file
(like a package) consisting of text, pictures and other types of data
(even
a video). What I would ideally like is something like this:

While brReader.PeekChar() > -1 ' until EOF
Read some data
Figure out what is in it (Text, part of an image. whatever)
Take the text part and put it in a label to display (as above)
Store the binary part in a variable (or append if I already have some
in
there)
... and keep going in this loop till I reach the end of the binary
part
Put the binary part in a PictureBox (assuming for the moment it is an
image)
Back to the top of the loop to read some more data (could be more
text
or another picture or whatever)
End While

Of course, I am the creator of the file (I wrote another program to write
these types of files), so I can put markers that tell me where the text
ends
and the image part starts etc.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Olie said:
It all depends on what type of data you want to read from the Binary
file and the type of that data. The example you have quoted simply
reads a single byte at a time out of the binary file which is not very
usful and very inefficient if you want to read a whole string or image.

I would recomend you look at some of the functions of BinaryReader as
it is designed to perform the file parsing and can realy help you. I
have to guess here as I do not know the format of your file but here is
an example:

UINT16 DataType
UINT32 DataSize
byte[] Data

With this structure of data in the binary file I would call these
functions on the BinaryReader object:

DataType = brReader.ReadUInt16()
DataSize = brReader.ReadUInt32()

and assuming the data is a string

string = brReader.ReadString()

or

MyByteArray = brReader.ReadBytes(DataSize)
string = MyByteArray.ToString()

You would obviously need to put in some checking to see when the end of
the file has been reached.


Anil Gupte wrote:
I am learning how to read and write files. Text files were easy, but
binary
files have me confused. As per the tutorial, I used BinaryReader to
read
a
file and put the contents into a ByteArray:

Dim fsReadStream As New FileStream(MyFileName, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte
While brReader.PeekChar() > -1
ByteArray = brReader.ReadBytes(1)

' This is the part where I don't know how to do

End While
brReader.Close()

How do I actually use the data in that ByteArray. I have basically
created
a file containing some text, some images and some video (I plan to
encrypt
this later). Now I want to open the file and read it, then parse it,
put
the text into a label, put the image into a picturebox etc. Any
tutorials
you can point me to, or any information at all will be useful.

Thanx,
 

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