469,593 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

Read Hex value from Specific address

I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the
different offset addresses for each portion of the data I am trying to retrieve. But, I am having problems actually reading the
data for the number of Bytes that I need to get from each address.
Here is some of the code I am currently working with:
Dim fsread As FileStream = File.Open(OpenFileDialog1.FileName, FileMode.Open)

Dim add As String

add = fsread.Seek(&HD0, SeekOrigin.Begin)

RichTextBox1.Text = add

Catch ex As Exception

'display error messages if they appear

MessageBox.Show(ex.Message)

End Try

End If

Using the above code, I get the Dec value for D0 ( 208) , instead of reading the contents of the data at that particular
address.

Any ideas on what I am doing wrong?

james


Nov 21 '05 #1
8 6974
I am confused by your post

Are you having problems actiually reading a specific set block size of data?

Also, your RichTextBox only holds one piece of data & isn't appended to it.

RichTextBox.Text = add

Don't you mean:

RichTextBox.Text += add ' Keep appending, but you need to use a loop to do
it this way.

You also state 'SeekOrigin.Begin'. What about 'SeekOrigin.Current'?

How big is the data you want to read? How big is the blocksize you want to
read?

Please just clarify

"james" wrote:
I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the
different offset addresses for each portion of the data I am trying to retrieve. But, I am having problems actually reading the
data for the number of Bytes that I need to get from each address.
Here is some of the code I am currently working with:
Dim fsread As FileStream = File.Open(OpenFileDialog1.FileName, FileMode.Open)

Dim add As String

add = fsread.Seek(&HD0, SeekOrigin.Begin)

RichTextBox1.Text = add

Catch ex As Exception

'display error messages if they appear

MessageBox.Show(ex.Message)

End Try

End If

Using the above code, I get the Dec value for D0 ( 208) , instead of reading the contents of the data at that particular
address.

Any ideas on what I am doing wrong?

james


Nov 21 '05 #2
Sorry about the confusion. Eventually, I will need to go thru the entire file. But, first I need to retreive different values at
different addresses in the header of the file(s) that will give me other information that I am looking for.
Well, to be clearer, I am working on reading an old Dataflex database .DAT file(s) (63 Tables in 63 seperate files, all
different sizes and number of fields). At location H08 is the number of rows in the file, and at H59 the number of fields.
Also, there are addresses that contain the field type for each field and the field size for each field. Also, there is an
address that contains the size of the record After that comes a null record. Then the actual field data.
The field names are in a seperate, Text file.
I can open the .TAG file that contains the field names and I have the names for the fields in each table. (seperate .TAG file
for each table) Then, I need to open the .DAT file and extract the information to build the rest of the table and save it to an
Access database.
The reason I am not looping thru the file or just reading to the end, is I wanted to be able to actually go to a specific
address and read the value at that address correctly. Once I can accurately do that, I feel that I can then proccess the rest of
the file with little to no problems. As for using SeekOrigin.Begin or SeekOrigin.Current, that was just one of the options that
Intellisense offered that I have tried using to read the value(s) I needed. Nothing carved in stone as to using that specific
function.
As I said in my original post, I am open to suggestions on how to this. I am not stuck on using FileStream or anything else.
james

"Crouchie1998" <Cr**********@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
I am confused by your post

Are you having problems actiually reading a specific set block size of data?

Also, your RichTextBox only holds one piece of data & isn't appended to it.

RichTextBox.Text = add

Don't you mean:

RichTextBox.Text += add ' Keep appending, but you need to use a loop to do
it this way.

You also state 'SeekOrigin.Begin'. What about 'SeekOrigin.Current'?

How big is the data you want to read? How big is the blocksize you want to
read?

Please just clarify

"james" wrote:
I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the
different offset addresses for each portion of the data I am trying to retrieve. But, I am having problems actually reading
the
data for the number of Bytes that I need to get from each address.
Here is some of the code I am currently working with:
Dim fsread As FileStream = File.Open(OpenFileDialog1.FileName, FileMode.Open)

Dim add As String

add = fsread.Seek(&HD0, SeekOrigin.Begin)

RichTextBox1.Text = add

Catch ex As Exception

'display error messages if they appear

MessageBox.Show(ex.Message)

End Try

End If

Using the above code, I get the Dec value for D0 ( 208) , instead of reading the contents of the data at that particular
address.

Any ideas on what I am doing wrong?

james


Nov 21 '05 #3
james wrote:
Dim fsread As FileStream = File.Open(OpenFileDialog1.FileName, FileMode.Open) Dim add As String add = fsread.Seek(&HD0, SeekOrigin.Begin)


The seek method does not read data, it only positions the file pointer
to the location you want. You must call the read method to actually
read data.

Nov 21 '05 #4
Chris, thank you. I guess I did not understand the .Seek method correctly.
One other question. Can I use a Hex value ( like the &H0 in my code sample) to set the file pointer? Or do I need to convert
that to a decimal value first?
james

"Chris Dunaway" <du******@gmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...
james wrote:
Dim fsread As FileStream = File.Open(OpenFileDialog1.FileName,

FileMode.Open)
Dim add As String

add = fsread.Seek(&HD0, SeekOrigin.Begin)


The seek method does not read data, it only positions the file pointer
to the location you want. You must call the read method to actually
read data.

Nov 21 '05 #5
I think using a hex constant is fine.

Nov 21 '05 #6
Just tried it like this:

Dim filename As String = OpenFileDialog1.FileName

Dim fs As FileStream

fs = New FileStream(filename, FileMode.Open, FileAccess.Read)

Dim r As StreamReader = New StreamReader(fs)

Dim sz As Integer = fs.Length

Dim mybytearray() As Byte = New Byte(sz) {}

Dim myencoding As ASCIIEncoding = New ASCIIEncoding

r.BaseStream.Seek(&H2D0, SeekOrigin.Current)

r.BaseStream.Read(mybytearray, 0, 7)

ListBox1.Items.Add(myencoding.GetString(mybytearra y))

r.Close()

fs.Close()

(converted from some C# code I found using Google)

This works really good. But, now I have another question. ( of course you knew that didn't you :-) )

In this line:

r.BaseStream.Read(mybytearray,0,7)

I am reading 7 characters and adding that to my listbox. And it works just fine. But, what I really want to do is to read

all the characters starting at &H2D0 till I encounter a Chr(32) (Space) and then stop. That way I can read variable length

strings (and or numbers) without having to know the exact length of the string I am reading. Is there any way to read the
stream

until it encounters a Space ( chr(32) ) or any other character I might specify?

james

"Chris Dunaway" <du******@gmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...
I think using a hex constant is fine.

Nov 21 '05 #7
The only way I know would be to read byte by byte until you hit a
space. An alternative would be to read a whole line using the ReadLine
method and then use the Split method to break out the string into
individual words.

Nov 21 '05 #8
Thanks Chris. I will give that a try.
james

"Chris Dunaway" <du******@gmail.com> wrote in message news:11**********************@c13g2000cwb.googlegr oups.com...
The only way I know would be to read byte by byte until you hit a
space. An alternative would be to read a whole line using the ReadLine
method and then use the Split method to break out the string into
individual words.

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Majordomo | last post: by
18 posts views Thread by jas | last post: by
5 posts views Thread by Jeong-Gun Lee | last post: by
8 posts views Thread by Ravindranath Gummadidala | last post: by
35 posts views Thread by hasho | last post: by
19 posts views Thread by Mark Richards | last post: by
1 post views Thread by Magix | last post: by
8 posts views Thread by a | last post: by
12 posts views Thread by kath | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.