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(inFile, 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(TimeStamp & " | " & 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
Greg_JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Göran AnderssonCommented:
Use the BitConverter class:

DataType = BitConverter.ToInt32(b, 16)
SequenceNumber = BitConverter.ToInt32(b, 20)
CompressedSize = BitConverter.ToInt32(b, 24)
DecompressedSize = BitConverter.ToInt32(b, 28)
0
Göran AnderssonCommented:
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
0
Göran AnderssonCommented:
> 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)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Greg_JohnsonAuthor Commented:
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
0
Greg_JohnsonAuthor Commented:
Here is the Sub incorporating your suggestions:

    Private Sub decodeHeader2(ByVal inFile As String)
        Dim inFileStream As New System.IO.FileStream(inFile, 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(TimeStamp & " | " & DataType & " | " & SequenceNumber & " | " & CompressedSize & " | " & DecompressedSize)

    End Sub
0
Göran AnderssonCommented:
To get the string, it should of course be the GetString method, not the GetBytes method.

TimeStamp = Encoding.ASCII.GetString(b, 0, 16)

My mistake... I was actually looking at the documentation for GetString and eventhough wrote GetBytes...


The binary data that you are reading have the integers stored in big endian format (Motorola) rather than little endian format (Intel). So you have to swap the bytes around before converting them:

Function ConvertToInt(a As Byte(), offset As Integer) As Integer
   Dim b(3) As Byte
   b(0) = a(offset + 3)
   b(1) = a(offset + 2)
   b(2) = a(offset + 1)
   b(3) = a(offset)
   Return BitConverter.ToInt32(b)
End Function

Now just change the calls to BitConverter.ToInt32 to call ConvertToInt instead.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Greg_JohnsonAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.