Advertisement

02.03.2008 at 03:25AM PST, ID: 23133054
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

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

Asked by Greg_Johnson in Microsoft Visual Basic.Net

Tags: , , ,

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 FunctionStart Free Trial
 
 
[+][-]02.03.2008 at 06:47AM PST, ID: 20808787

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.03.2008 at 06:56AM PST, ID: 20808810

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.03.2008 at 07:06AM PST, ID: 20808840

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.03.2008 at 08:27AM PST, ID: 20809057

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.03.2008 at 08:30AM PST, ID: 20809067

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.03.2008 at 09:02AM PST, ID: 20809175

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual Basic.Net
Tags: Microsoft, VB.NET, VB.NET 2003, Conversion of Binary data to an Integer
Sign Up Now!
Solution Provided By: GreenGhost
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628