Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manipulating binary data

Status
Not open for further replies.

geezer34

Technical User
Mar 17, 2004
11
GB
I'm a novice VB programmer and am having some issues with binary files (which I've never touched before) and I was hoping you may be able to help with...

I've read a binary file into a byte array and now want to pull out data from this.

I know the following about my format:

- byte position 0 is an ascii Char
- byte position 1,2,3 refers to an unsigned 24 bit (I want to put this in a long)
- bytes poistion 4,5 refers to a signed 16 bit numner ( I want to put this in an integer)

- byte 6 is an ascii char
etc etc

I believe data was written to the file using little endian if that helps.

I obvisouly need to loop through my array but how do I convert the data at the byte position into longs / integers??

Any code to illistrate would be great.

Thanks for you help
 
This is top-of-the-head, as I am away from help files and Vb at the moment..

In general terms, the last time I tried this, I had to resort to using custom Types

I had one type like this:

Public Type MyLong
v as long
end type

and another like this:

Public Type My4Bytes
b1 as byte
b2 as byte
b3 as byte
b4 as byte
end type


declaring a variable of type My4Bytes, I set the values of b1,b2, b3 etc.

The using LSET (or RSET, can't recall off hand), I forced the value in that into a variable of type MyLong

dim v1 as MyLong
dim v2 as My4Bytes

v2.b2 = 76
LSET v1 = v2

debug.print v1


You can do it mathematically, by mutliplying the values by 1, 256, 256^2 etc, but that gets messy to read very quickly...


 
Ok thanks for that.

Query re UDTs as I've got an error come up saying that LSET only allowed on strings and user defined data types...

Got the following code:

Private Type Test
a As String
b As Long
End Type

Dim record() As Test

Private Type My4Bytes
b1 As Byte
b2 As Byte
b3 As Byte
b4 As Byte
End Type

Dim v2 As My4Bytes

'after I have readin in my binary file I do

v2.b2 = myarray(counter + 1)
v2.b3 = myarray(counter + 2)
v2.b4 = myarray(counter + 3)
LSet record(index).b = v2 ** error here **

where index is the array index of the record I'm populating, and counter is the index where the binary data is stored in my data that I read in.

Any ideas what I've messed up here?

 
OK got something going here. Basicaly I created a load of UDTs and then used these within another UDT!!

Seems to work OK.

Thanks for your pointer
 
Glad to see you have it moving.

The line
LSet record(index).b = v2 ** error here **

is failing because the 'record(index).b' is not a UDT
Both sides of the equation have to be, but you have worked that out for yourself, I guess..

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top