[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Converting 4-Byte Binary data to Integer in VB.NET

Posted on 2008-02-03
7
Medium Priority
?
4,469 Views
Last Modified: 2011-10-19
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
0
Comment
Question by:Greg_Johnson
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20808787
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20808810
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20808840
> 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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 

Author Comment

by:Greg_Johnson
ID: 20809057
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
 

Author Comment

by:Greg_Johnson
ID: 20809067
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
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 2000 total points
ID: 20809175
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
 

Author Closing Comment

by:Greg_Johnson
ID: 31427597
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Jorge
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question