473,287 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Parsing binary data (byte[])

Hi,

I'm pretty new to C#, and recently I have been experimenting with sockets.
However, I can't find a nice way (I've been fiddling with for-loops to no
avail) to parse the data that comes in over the socket. I realise this is
a bit of a general subject and there must be some good articles on it, so
any pointers to a good article dealing with this are welcome.

One problem in particular was a response form a gameserver containing
status info. The response basically consists of ASCII values, where each
value has 1 byte prepended to it indicating the field's length (if the
value would have been 3 bytes, the length would be 4).

To make it slightly more complicated, the response also has a few 0x00
(SOH in ASCII) bytes in it, indicating different parts of the response
(kinda like a seperator, seperating general server info, server rules and
players). Also, the last part of the response has a few ASCII values
(player info) 'grouped' by having an extra byte (0x1d if memory serves me)
prepended.

Any help is greatly appreciated.
Nov 15 '05 #1
4 5565
Can you represent the packet as a struct? Post the actual data format of
the packet and maybe sample data and we could help better.

--
William Stacey, MVP

"Hans Nieser" <h.******@xs4all.invalid> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

I'm pretty new to C#, and recently I have been experimenting with sockets.
However, I can't find a nice way (I've been fiddling with for-loops to no
avail) to parse the data that comes in over the socket. I realise this is
a bit of a general subject and there must be some good articles on it, so
any pointers to a good article dealing with this are welcome.

One problem in particular was a response form a gameserver containing
status info. The response basically consists of ASCII values, where each
value has 1 byte prepended to it indicating the field's length (if the
value would have been 3 bytes, the length would be 4).

To make it slightly more complicated, the response also has a few 0x00
(SOH in ASCII) bytes in it, indicating different parts of the response
(kinda like a seperator, seperating general server info, server rules and
players). Also, the last part of the response has a few ASCII values
(player info) 'grouped' by having an extra byte (0x1d if memory serves me)
prepended.

Any help is greatly appreciated.

Nov 15 '05 #2
William Stacey wrote:
Can you represent the packet as a struct? Post the actual data format of
the packet and maybe sample data and we could help better.


This is an example packet I captured:

http://www.nieser.net/files/bf1942-status.png

And this is an attempt at writing down the data format:

http://www.nieser.net/files/bf1942-status.txt (but since I have no clue on
how to write this kind of thing out, I wouldn't be surprised if noone
understood)

Also I made an error in my first post by saying SOH is 0x00 while it is
actually 0x01.
Nov 15 '05 #3
You can use MemoryStream to ease data access, if you can do with streaming
data access to get blocks of data of known size. Later you can use
System.Text.Encoding members to convert this byte stream to readable values.
I would suggest to use streaming data access and no data caching for better
performance (Its for a computer game) , but actually if slow access is
alright, you can go for structure declarations and methods to create its
instance using a byte array or byte stream or whatever u prefer to use.
"Hans Nieser" <h.******@xs4all.invalid> wrote in message
news:3f*********************@news.xs4all.nl...
William Stacey wrote:
Can you represent the packet as a struct? Post the actual data format of the packet and maybe sample data and we could help better.


This is an example packet I captured:

http://www.nieser.net/files/bf1942-status.png

And this is an attempt at writing down the data format:

http://www.nieser.net/files/bf1942-status.txt (but since I have no clue on
how to write this kind of thing out, I wouldn't be surprised if noone
understood)

Also I made an error in my first post by saying SOH is 0x00 while it is
actually 0x01.

Nov 15 '05 #4
Hans Nieser <h.******@xs4all.invalid> wrote:
One problem in particular was a response form a gameserver containing
status info. The response basically consists of ASCII values, where each
value has 1 byte prepended to it indicating the field's length (if the
value would have been 3 bytes, the length would be 4).


Right - so for each value, look at the first byte to see how long the
whole value is (so you know where the next one starts). Then using
Encoding.ASCII.GetString(byte[], int, int) to decode just the right
portion of your byte array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
9
by: Stargate4004 | last post by:
Hi, I have am converting a VB6 application to VB.net and have run into a problem. The application connects to a TCP port on a Linux box and receives a data buffer. The data buffer contains...
2
by: Michael Kolias | last post by:
Hello everyone and happy holidays! I have a situation where I want to parse on the fly a stream of binary data. I am developing an httpmodule for asp.net for file uploading. I do not whant to...
8
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
4
by: Tom | last post by:
I am trying to update another developers code, and am stuck in a cludge. It works like this: A user uploads a file from the web page. Our code reads from a NetworkStream to a Filestream, and...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.