combining bytes

rogs95

Member
Joined
Mar 1, 2006
Messages
11
Programming Experience
1-3
Probably a simple answer, but i can't find it.

I am working with binary data and need to combine 2 bytes and read the value of the 16 bits between 0-65635.

Any help is appreciated
 
put the bytes in an array and use the BitConverter class
VB.NET:
Dim sh As Short = BitConverter.ToInt16(New Byte() {1, 6}, 0)
MsgBox(sh.ToString)
 
This is the code i am now using to combine 4 bytes into 32 bits, the 4 input bytes are 0, 7, 62 and 115, which when put together to 32 bits becomes 474739, however in the code it becomes 1933444864.

Does anyone notice anything wrong in the code or am i calculating it wrong?

VB.NET:
[SIZE=2]inFile = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
 
[/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2] binaryData(inFile.Length)[/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2]inFile.Seek(Basestart, SeekOrigin.Begin)
inFile.Read(binaryData, 0, inFile.Length)
 
 
[/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sh [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]sh = BitConverter.ToInt32(binaryData, 0)
[/SIZE]
 
bytes 0, 7, 62, 115 in that order is int32 1933444864
bytes 115, 62, 7, 0 in that order is int32 474739
 
The four bytes i'm trying to read are the size of the record that i am looking at, 1933444864 gives a record bigger than the file!

When writing the bytes out:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 3[/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2]Console.WriteLine(binaryData(i))
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]

it prints the bytes as
0
7
62
115


474739 seems correct. As the image contained in the record has the height and width of 689*689 (taken from two bytes combined 2, 177), which gives 474721 bytes.

How do i get the code to read the bytes backwards to get the correct size.
 
Sounds strange, but bytes 177&2 is 689 as you say (2&177 is -20222 for int16 or 45314 for Uint16).
Shouldn't be a problem putting the four bytes into a four byte array in reverse order?
VB.NET:
Dim fourbytes(3) As Byte
For i As Integer = 0 To 3
  fourbytes(3 - i) = binaryData(i)
Next
Dim sh As Integer = BitConverter.ToInt32(fourbytes, 0)


Could someone have played you a trick with this file?
 
I don't believe so, i'm creating a viewer to view the images from a file, i know for definate that this file meets the standard.

The first two record types in the file are ASCII data, record one has byte length 247 and record two has length 108, which equals 373.

The next record is the one that i am looking at which is binary, the first 18 bytes contain info, such as logical record length, horizontal line length and vertical line length. The first four bytes contain the size of the record:



VB.NET:
[FONT=Arial][SIZE=2]
 
[LEFT]This mandatory four-byte binary field shall occupy

bytes one through four. It shall contain the length
[LEFT]of the logical record specifying the total number of
bytes, including every byte of all nine fields contained
in the record.[/LEFT]
 

[/LEFT]
[/SIZE][/FONT]



i'm assuming that reading them backwards is correct as it makes the data match as:​

115, 62, 7, 0 gives 474739
177, 2 gives 689 = the height and width​

689*689 gives 474721​

474739 minus the first 18 info bytes = 474721​

As these values match, i'm assuming its OK.​

Now i just need to use the 474721 bytes to create a bitmap (see other thread that your also helping me with)​

Looking at the bitmap structure:
HTML:
http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.htm

it seems that the 474721 bytes is missing the bitmap file header, as it seems to do you know if this is needed, or can a bitmap be created straight from the bytes?​

 
One would think record three is the complete bitmap and that the values you have read is part of the header info.
 
don't think so in the standard it read that field 9 (after the 18 bytes) is:

VB.NET:
[B][FONT=Arial Narrow][SIZE=3]
[LEFT]11.1.9 Image data
 [/LEFT]
[/B][/SIZE][/FONT][FONT=Arial][SIZE=2][LEFT]This binary field shall contain all of the highresolution
grayscale image data. Each pixel of
the uncompressed image shall be quantized to
eight bits (256 gray levels) contained in a single
byte.[/LEFT]
[/SIZE][/FONT]

The fields located before the image do not match the header description, as other info is held that does not relate directly to the image.
 
That quote doesn't contradict that the image data contained is a fully qualified bitmap/dib. Try it out with code you refered to (this thread http://www.vbdotnetforums.com/showthread.php?t=8537 reading image bytes), and do post back your findings there.

Here is the reference rogs95 is talking about, maybe someone got more specific info about working with this.
"American National Standard for Information Systems — Data Format for the Interchange of Fingerprint, Facial, & Scar Mark & Tattoo (SMT) Information" http://www.griaule.com/public_download/sp500-245-a16.pdf
 
Back
Top