Greg_Johnson
asked on
Converting 4-Byte Binary data to Integer in VB.NET
I need to decode a fixed length header on a packet of compressed data. The header is always 32 bytes. The header starts with 16 byte null terminated ASCII timestamp. This is followed by four binary 4-byte integer values for the data packet type, sequence number (for making sure we're not missing packets), the compressed size, and the decompressed size.
Getting the ASCII data isn't an issue and the code below works fine on small numbers with the binary data (the packet type and sequence numbers are both less than 100 and work fine with the code below) but as the compressed/decompressed sizes go up, I don't get the right results.
I believe this is related to the encoding and the lack of support for ASCb in VB.NET? You can see that I am using UTF8 encoding and then I use ASC to determine the character value. Here is my code:
Private Sub decodeHeader2(ByVal inFile As String)
Dim inFileStream As New System.IO.FileStream(inFil e, System.IO.FileMode.Open)
Dim b(33) As Byte
Dim S As String
Dim DataType As Integer
Dim SequenceNumber As Integer
Dim TimeStamp As String
Dim CompressedSize As Integer
Dim DecompressedSize As Integer
Dim Output As String
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Console.WriteLine("Byte Length: " & b.Length)
Try
Do While inFileStream.Read(b, 0, b.Length) > 0
S = temp.GetString(b)
Loop
Finally
inFileStream.Close()
End Try
Console.WriteLine("Length: " & Len(S))
TimeStamp = Mid(S, 1, 14)
DataType = bin2int(Mid(S, 17, 4))
SequenceNumber = bin2int(Mid(S, 21, 4))
CompressedSize = bin2int(Mid(S, 25, 4))
DecompressedSize = bin2int(Mid(S, 29, 4))
Console.WriteLine(TimeStam p & " | " & DataType & " | " & SequenceNumber & " | " & CompressedSize & " | " & DecompressedSize)
End Sub
Private Function bin2int(ByVal str As String) As Integer
Dim temp As Integer
Dim strlen As Integer
Dim i As Integer
temp = 0
strlen = Len(str)
Console.WriteLine("Str Len: " & Len(str))
For i = 1 To strlen
temp = temp * 256 + (Asc(Mid(str, i, 1)))
Next
bin2int = temp
End Function
Getting the ASCII data isn't an issue and the code below works fine on small numbers with the binary data (the packet type and sequence numbers are both less than 100 and work fine with the code below) but as the compressed/decompressed sizes go up, I don't get the right results.
I believe this is related to the encoding and the lack of support for ASCb in VB.NET? You can see that I am using UTF8 encoding and then I use ASC to determine the character value. Here is my code:
Private Sub decodeHeader2(ByVal inFile As String)
Dim inFileStream As New System.IO.FileStream(inFil
Dim b(33) As Byte
Dim S As String
Dim DataType As Integer
Dim SequenceNumber As Integer
Dim TimeStamp As String
Dim CompressedSize As Integer
Dim DecompressedSize As Integer
Dim Output As String
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Console.WriteLine("Byte Length: " & b.Length)
Try
Do While inFileStream.Read(b, 0, b.Length) > 0
S = temp.GetString(b)
Loop
Finally
inFileStream.Close()
End Try
Console.WriteLine("Length:
TimeStamp = Mid(S, 1, 14)
DataType = bin2int(Mid(S, 17, 4))
SequenceNumber = bin2int(Mid(S, 21, 4))
CompressedSize = bin2int(Mid(S, 25, 4))
DecompressedSize = bin2int(Mid(S, 29, 4))
Console.WriteLine(TimeStam
End Sub
Private Function bin2int(ByVal str As String) As Integer
Dim temp As Integer
Dim strlen As Integer
Dim i As Integer
temp = 0
strlen = Len(str)
Console.WriteLine("Str Len: " & Len(str))
For i = 1 To strlen
temp = temp * 256 + (Asc(Mid(str, i, 1)))
Next
bin2int = temp
End Function
Also, there is a problem with how you are reading the file. You are right in that you should call the Read method as long as it returns a value larger than zero, but you have to read the data into the array at different positions, otherwise the second block will overwrite the first. As you don't have any check for how much data you read, your current code will read the entire file, so you don't get the header at all, but some arbitrary data at the end of the file.
This code will read binary data from the file until you reach the end of the file or the size of the array:
Dim pos As Integer = 0
Do
s = inFileStream.Read(b, pos, b.Length - pos)
pos += s
Loop Until s = 0 or pos = b.Length
The pos variable will contain the amount of data read. You should check this value so that you are sure that you really got the amount of data that you expect.
And, you are declaring the array to 34 bytes, not 32 bytes. When you declare an array you give the index of the last item, so for a 32 byte array the last index is 31:
Dim b(31) As Byte
This code will read binary data from the file until you reach the end of the file or the size of the array:
Dim pos As Integer = 0
Do
s = inFileStream.Read(b, pos, b.Length - pos)
pos += s
Loop Until s = 0 or pos = b.Length
The pos variable will contain the amount of data read. You should check this value so that you are sure that you really got the amount of data that you expect.
And, you are declaring the array to 34 bytes, not 32 bytes. When you declare an array you give the index of the last item, so for a 32 byte array the last index is 31:
Dim b(31) As Byte
> I believe this is related to the encoding and the lack of support for ASCb in VB.NET?
There is no binary strings in .NET, that's why there is no ASCB function in VB.NET. Strings in .NET are unicode, so each character is a 16 bit value.
To handle binary data you use byte arrays, not strings. If you decode the data into a string, the values will change, so some character values will be different than the byte values they were decoded from. Also, UTF8 uses more than one byte to encode some characters, so if you decode an array of 32 bytes, you don't always get a 32 character string.
To get the ASCII string from the data, you should use the ASCII encoding, not UTF8:
TimeStamp = Encoding.ASCII.GetBytes(b, 0, 16)
There is no binary strings in .NET, that's why there is no ASCB function in VB.NET. Strings in .NET are unicode, so each character is a 16 bit value.
To handle binary data you use byte arrays, not strings. If you decode the data into a string, the values will change, so some character values will be different than the byte values they were decoded from. Also, UTF8 uses more than one byte to encode some characters, so if you decode an array of 32 bytes, you don't always get a 32 character string.
To get the ASCII string from the data, you should use the ASCII encoding, not UTF8:
TimeStamp = Encoding.ASCII.GetBytes(b,
ASKER
Hi and Thanks GreenGhost;
I incorporated your changes and have the following to report:
With "TimeStamp = Encoding.ASCII.GetBytes(b, 0, 16)" I get a compile error:
Value of type '1-dimensional array of Byte' cannot be converted to '1-dimensional array of Char' because 'Byte' is not derived from 'Char'
Using bitconverter didn't error out but I'm not getting the results that I am looking for. I am going to try to attach a sample binary header file and a txt file with the expected results for you to have a look at.
sample-header-002.txt
sample-header-002-.log
I incorporated your changes and have the following to report:
With "TimeStamp = Encoding.ASCII.GetBytes(b,
Value of type '1-dimensional array of Byte' cannot be converted to '1-dimensional array of Char' because 'Byte' is not derived from 'Char'
Using bitconverter didn't error out but I'm not getting the results that I am looking for. I am going to try to attach a sample binary header file and a txt file with the expected results for you to have a look at.
sample-header-002.txt
sample-header-002-.log
ASKER
Here is the Sub incorporating your suggestions:
Private Sub decodeHeader2(ByVal inFile As String)
Dim inFileStream As New System.IO.FileStream(inFil e, System.IO.FileMode.Open)
Dim b(31) As Byte
Dim S As String
Dim DataType As Integer
Dim SequenceNumber As Integer
Dim TimeStamp As String
Dim CompressedSize As Integer
Dim DecompressedSize As Integer
Dim Output As String
Console.WriteLine("Byte Length: " & b.Length)
Dim pos As Integer = 0
Do
S = inFileStream.Read(b, pos, b.Length - pos)
pos += S
Loop Until S = 0 Or pos = b.Length
Console.WriteLine("Read Length: " & S)
TimeStamp = Encoding.ASCII.GetBytes(b, 0, 16)
DataType = BitConverter.ToInt32(b, 16)
SequenceNumber = BitConverter.ToInt32(b, 20)
CompressedSize = BitConverter.ToInt32(b, 24)
DecompressedSize = BitConverter.ToInt32(b, 28)
Console.WriteLine(TimeStam p & " | " & DataType & " | " & SequenceNumber & " | " & CompressedSize & " | " & DecompressedSize)
End Sub
Private Sub decodeHeader2(ByVal inFile As String)
Dim inFileStream As New System.IO.FileStream(inFil
Dim b(31) As Byte
Dim S As String
Dim DataType As Integer
Dim SequenceNumber As Integer
Dim TimeStamp As String
Dim CompressedSize As Integer
Dim DecompressedSize As Integer
Dim Output As String
Console.WriteLine("Byte Length: " & b.Length)
Dim pos As Integer = 0
Do
S = inFileStream.Read(b, pos, b.Length - pos)
pos += S
Loop Until S = 0 Or pos = b.Length
Console.WriteLine("Read Length: " & S)
TimeStamp = Encoding.ASCII.GetBytes(b,
DataType = BitConverter.ToInt32(b, 16)
SequenceNumber = BitConverter.ToInt32(b, 20)
CompressedSize = BitConverter.ToInt32(b, 24)
DecompressedSize = BitConverter.ToInt32(b, 28)
Console.WriteLine(TimeStam
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
GreenGhost - Thanks very much for your help! I haven't had to do this type of conversion before and you got me squared away very quickly... You Rock!
DataType = BitConverter.ToInt32(b, 16)
SequenceNumber = BitConverter.ToInt32(b, 20)
CompressedSize = BitConverter.ToInt32(b, 24)
DecompressedSize = BitConverter.ToInt32(b, 28)