Read Hex value from Specific address

J

james

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
 
G

Guest

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
 
J

james

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
 
C

Chris Dunaway

james said:
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.
 
J

james

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
 
J

james

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(mybytearray))

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
 
C

Chris Dunaway

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.
 

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