Working with binary data?

D

Dan

To all the gurus out there. I am writing a tool that
receives binary data from a network device. The data
arrives in a standard format which the vendor has
documented, e.g. byte 0 is the format version, 1-4 are
integers between 0 and 255, etc. At this point I just
want to format the data into something that can be read
by humans and write it to the console. I have seen some
other examples of how people acomplish this but none in
visual basic. There is a Perl example I ran across that
uses the unpack operation to change the binary data to
several strings. I have tried converting the data to a
string with different types of encoding. No luck with
that it just shows up as jibberish. I have included my
source so you can see what i'm after. Is there a
comperable operation to the Perl unpack or some little
hack routine that someone has written to make the binary
data useful? Thanks!

Dan

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class NetFlowCollector

Private Shared UDPPort As Integer = 5000

Private Shared Sub StartListener()
Dim done As Boolean = False

Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any,
0)
Dim UDPClient As New UdpClient(UDPPort)

Try
While Not done
Dim bytes As Byte() = UDPClient.Receive
(RemoteIpEndPoint)

Console.WriteLine(strData)
End While

Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub

Public Overloads Shared Function Main(ByVal args() As
[String]) As Integer
Console.WriteLine("Listening for NetFlow data on UDP
{0}", UDPPort)
StartListener()

Return 0
End Function 'Main
End Class
 
D

Dan

Thanks for the reply. That seems to have me moving in
the right direction. Can you recommend the most
efficient way to feed that information "into" an say
ASCII? So right now I dump the info to the console and I
see:

00-34-00-00-00...

I assume that is hex which each group representing one
byte. I'm afraid that if i convert that data into a
string and then parse it out with a hex2decimal function
and then feed it to ascii that will be too slow. Thanks
again!

Dan

-----Original Message-----
Try the BitConverter class. You can pack and unpack
bytes this way.
 
B

Brian

This will do it.

Dim b As BitConverter
Dim test(5) As Byte

test(0) = 72
test(1) = 69
test(2) = 76
test(3) = 76
test(4) = 79

Dim str As String = Encoding.ASCII.GetString(test, 0, test.Length)
Debug.WriteLine(str)
 
G

Guest

I'm going to have to beg forgiveness but I think you are
missing something in your example. You have Dim b As
BitCoverter but I don't see b referenced anywhere else in
the example. I think that is what i'm missing.

Thanks,

Dan
 
B

Brian

That was a mistake, I just forgot to erase it. Here's a clearer example:

'-- Declare your array
Dim temp() As Byte

'-- This is your recieved Integer (just a test)
Dim k As Integer = 311875

'-- Convert the Integer to a byte array
temp = BitConverter.GetBytes(k)

'-- Now, get the string representation
Dim str As String = Encoding.ASCII.GetString(temp, 0, temp.Length)

Debug.WriteLine(str)
 
D

Dan

BTW

When I am running my last bit of code:

Console.WriteLine(BitConverter.ToUInt16(bytData, 0))

The result is 256. When I look at the watch window I see:

Byte0 0
Byte1 1
Byte2 0
Byte3 24
etc.

Dan
 
B

Brian

Bytes Contents
0-1 Version ---------> Short Data Type (Int16)
2-3 Count ---------> Short Data Type (Int16)
4-7 Uptime ----------> Integer Data Type (Int32)
8-11 Seconds ----------> Integer Data Type (Int32)
12-16 uSeconds --------> Integer DataType (Int32)
 
B

Brian

Here are the values of the first two pair of bytes when packed into a Int16.

Byte0 0
Byte1 1----------- = 256 (first 2 bytes)

Byte2 0
Byte3 24---------- = 6144(second 2 bytes)
 
D

Dan

I see exactly what you are talking about with the bytes
and the values that I see when I convert to .ToInt16. I
am clearly misunderstanding why though. The numbers 1
and 24 are the expected results. If I see this:

Byte0 0
Byte1 1----------- = 256 (using .ToInt16)

Byte2 0
Byte3 24---------- = 6144(using .ToInt16)

Then what type of operation is taking place, or needs to
take place to get the expected results of 1 and 24. If i
take the numbers that I get 256 and 6144 and do some math
I get:

Int(256/256) = 1
Int(6114/256) = 24

Is there some 'factor' that needs to be applied to get
the expected results after using .ToIntXX?

Thanks again!
Dan
 
B

Brian

Here's the story of packing 2 bytes into a short value. Maybe this will
help. Sometimes knowing how it works will help you see it more clearly.

Example : Byte1 is shifted to the left 8 positions (or multiply it by 256)
1 << 8 (= 256) then byte0 is added (0 ) for a total of 256 + 0 = 256

If you do the opposite and shift the number 256 >> 8 positions to the right
you get 1

Example 2: Byte3 is shifted to the left 4 positions 24 << 8 (=6144) then
byte2 is added. Since byte2s value is 0 then the final number is 6144 + 0 =
6144

If you take the number 6144 >> 8 and shift it right 8 positions you get 24


Example 3: Two bytes with values of 255 and 1. To create a packed short
value (1 << 8) then OR (or add) it with 255

Dim test as Short

test = (1 << 8) OR 255 '-- Result is 511

' -- Another way instead of shifting
test = (1 * 256 ) + 255 '-- Result is 511

' -- Yet another way
test = (1 << 8) OR &HFF '-- Result is 511

' -- Yet another way: Part 2
test = (1 * 256 ) + &HFF '-- Result is 511
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top