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
Start Free Trial