473,657 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Working with binary data?

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.Sock ets
Imports System.Text

Public Class NetFlowCollecto r

Private Shared UDPPort As Integer = 5000

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

Dim RemoteIpEndPoin t As New IPEndPoint(IPAd dress.Any,
0)
Dim UDPClient As New UdpClient(UDPPo rt)

Try
While Not done
Dim bytes As Byte() = UDPClient.Recei ve
(RemoteIpEndPoi nt)

Console.WriteLi ne(strData)
End While

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

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

Return 0
End Function 'Main
End Class
Nov 20 '05 #1
10 1575
Try the BitConverter class. You can pack and unpack bytes this way.

"Dan" <an*******@disc ussions.microso ft.com> wrote in message
news:03******** *************** *****@phx.gbl.. .
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.Sock ets
Imports System.Text

Public Class NetFlowCollecto r

Private Shared UDPPort As Integer = 5000

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

Dim RemoteIpEndPoin t As New IPEndPoint(IPAd dress.Any,
0)
Dim UDPClient As New UdpClient(UDPPo rt)

Try
While Not done
Dim bytes As Byte() = UDPClient.Recei ve
(RemoteIpEndPoi nt)

Console.WriteLi ne(strData)
End While

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

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

Return 0
End Function 'Main
End Class

Nov 20 '05 #2
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.

Nov 20 '05 #3
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)
"Dan" <an*******@disc ussions.microso ft.com> wrote in message
news:01******** *************** *****@phx.gbl.. .
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.

Nov 20 '05 #4
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

-----Original Message-----
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.WriteLin e(str)
"Dan" <an*******@disc ussions.microso ft.com> wrote in messagenews:01******* *************** ******@phx.gbl. ..
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.

.

Nov 20 '05 #5
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.Ge tBytes(k)

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

Debug.WriteLine (str)
<an*******@disc ussions.microso ft.com> wrote in message
news:02******** *************** *****@phx.gbl.. .
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

-----Original Message-----
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.WriteLin e(str)
"Dan" <an*******@disc ussions.microso ft.com> wrote in

message
news:01******* *************** ******@phx.gbl. ..
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.

.

Nov 20 '05 #6
Dan
BTW

When I am running my last bit of code:

Console.WriteLi ne(BitConverter .ToUInt16(bytDa ta, 0))

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

Byte0 0
Byte1 1
Byte2 0
Byte3 24
etc.

Dan
-----Original Message-----
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.Ge tBytes(k)

'-- Now, get the string representation
Dim str As String = Encoding.ASCII. GetString(temp, 0, temp.Length)
Debug.WriteLin e(str)
<an*******@dis cussions.micros oft.com> wrote in message
news:02******* *************** ******@phx.gbl. ..
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

>-----Original Message-----
>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.WriteLin e(str)
>
>
>"Dan" <an*******@disc ussions.microso ft.com> wrote in

message
>news:01******* *************** ******@phx.gbl. ..
>> 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.
>>
>
>
>.
>

.

Nov 20 '05 #7
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)


"Dan" <an*******@disc ussions.microso ft.com> wrote in message
news:07******** *************** *****@phx.gbl.. .
BTW

When I am running my last bit of code:

Console.WriteLi ne(BitConverter .ToUInt16(bytDa ta, 0))

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

Byte0 0
Byte1 1
Byte2 0
Byte3 24
etc.

Dan
-----Original Message-----
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.Ge tBytes(k)

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

temp.Length)

Debug.WriteLin e(str)
<an*******@dis cussions.micros oft.com> wrote in message
news:02******* *************** ******@phx.gbl. ..
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
>-----Original Message-----
>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.WriteLin e(str)
>
>
>"Dan" <an*******@disc ussions.microso ft.com> wrote in
message
>news:01******* *************** ******@phx.gbl. ..
>> 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.
>>
>
>
>.
>

.

Nov 20 '05 #8
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)

"Dan" <an*******@disc ussions.microso ft.com> wrote in message
news:07******** *************** *****@phx.gbl.. .
BTW

When I am running my last bit of code:

Console.WriteLi ne(BitConverter .ToUInt16(bytDa ta, 0))

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

Byte0 0
Byte1 1
Byte2 0
Byte3 24
etc.

Dan
-----Original Message-----
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.Ge tBytes(k)

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

temp.Length)

Debug.WriteLin e(str)
<an*******@dis cussions.micros oft.com> wrote in message
news:02******* *************** ******@phx.gbl. ..
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
>-----Original Message-----
>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.WriteLin e(str)
>
>
>"Dan" <an*******@disc ussions.microso ft.com> wrote in
message
>news:01******* *************** ******@phx.gbl. ..
>> 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.
>>
>
>
>.
>

.

Nov 20 '05 #9
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 misunderstandin g 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
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
15222
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
103
48597
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
2
2520
by: Lisa Pearlson | last post by:
Hi, My php application (on Apache/Linux) needs to do the following: The PHP script receives a request from a client (binary), asking for certain records of data. My PHP script loops through all records and sends each of them ONE BY ONE. After each record that my server script sends, it waits for the client to confirm proper reception with an ACK (binary digit). When there are no more records, my server script sends the client a binary
28
2776
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
4
3684
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the following code that compiles OK. It runs and shows the requested output. However, after execution, it pops one of those windows to send error reports online to the porgram creator. I have managed to find where the error is but can't see what's wrong....
6
1582
by: Larry Serflaten | last post by:
Acording to Bob Powell, serializing an object should be a breeze: http://groups.google.com/groups?hl=en&lr=&safe=off&selm=%23TR3qvCcCHA.2544%40tkmsftngp11 But its not happening, and I can't see why not. When I save the file, it does not have near enough data to contain the object, so, no way will I be able to deserialize it. Can anyone see where I went wrong? No errors are reported, but I see no image being saved, or loaded in. ...
3
6991
by: stockblaster | last post by:
Hello all.. Is it possible to convert a DataTable (i create the DataTable from a CSV file) into binary data and save it into an sql 2005 table (into binary field). After that I want to have the ability to add a row to the beginning of the to the binary data..
5
4321
by: RobinS | last post by:
I want to serialize a class that I am using to retain some information the user types into a screen. I have 3 questions. 1) I serialized it as XML to start with. This works, but how do I serialize the strings so that they are not messed up if they have XML in them, or control characters? Is there a way to do that in XML, or do I have to use BinaryFormatters? 2) So I tried using a binary formatter, and it won't serialize/deserialize...
9
10024
by: Aamir Mahmood | last post by:
Hi, I have working on a system in which I have to manipulate *very* big numbers. Like 32368060745625089670148189374568111100874165870871388541651800834565616109380834613212956588769877 They may be upto 10000 digits long. These numbers are coming through a device in ascii format, I am creating a text file and saving these numbers in the file.
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.