Binary help please

  • Thread starter Thread starter John Dann
  • Start date Start date
J

John Dann

I'm trying to read some data from a disk file created by a third party
application (written I think in C), so I don't have any option other
than to read/interpret the data in the format in which it's been
saved. There's one particular field giving me problems:

The problematic data item is a 2-byte field saved as an unsigned
integer and has two problems:

1. I know that I can't read a uint directly in to VB.Net (Framework
1.1 at least) as a native type. But presumably there's nothing
stopping me reading it as a short and (?) subtracting 32768 to get the
original value? But see also the next problem:

2. The 2-byte field is actually holding two data items. The most
significant nybble (which I presume is the top 4 bits) holds one (say
A), with the remaining 12 bits (?) holding the other (B). I have some
documentation that suggests retrieving these values by using:

A = uint & 0xF000

B = uint & 0x0FFF

But I must admit to being stuck as to how to implement these in
VB.Net. I'm guessing that it should be possible to do this either
directly or with a workaround, but what?

Any help much appreciated.

JGD
 
John:

Have you looked into System.IO.BinaryReader? It has a slew of methods to
read bits and bytes.

Venkat
 
John:

Have you looked into System.IO.BinaryReader? It has a slew of methods to
read bits and bytes.

Yes I'm actually using BinaryReader to read all of this binary file. I
am reading in the 2-byte field in question as a short, which works
fine (in the sense of not giving any error or interrruption to the
file-reading process). But AFAIK there's no method for reading a uint.
And maybe I could read in the individual bits, but I'd still have the
problem of reconstituting the variables, which is really where I'm
stuck..

I'm fairly sure there are ways to deal with this reading it as a
short, but because I hardly ever have to deal with bit manipulation,
it's not an area of VB where I've much competence/confidence.

JGD
 
vvenk said:
Have you looked into System.IO.BinaryReader? It has a slew of methods to
read bits and bytes.

Is slew the right word? Or should that be a plethora? <g>

(From the current .Net Show... ;-)

LFS
 
John Dann said:
Yes I'm actually using BinaryReader to read all of this binary file. I
am reading in the 2-byte field in question as a short, which works
fine (in the sense of not giving any error or interrruption to the
file-reading process).

I'd suggest you read it in as 2 Bytes, and then just move 4 bits
from one byte to the other as you convert them to Integers:

Dim b1, b2 As Byte
Dim i1, i2 As Integer

b1 = 124
b2 = 39

i1 = b1 \ &H10
i2 = ((b1 And &HF) * &H10000) + b2


HTH
LFS
 

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

Back
Top