473,569 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5602
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.******@xs4al l.invalid> wrote in message
news:3f******** *************@n ews.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.Enc oding 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.******@xs4al l.invalid> wrote in message
news:3f******** *************@n ews.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.******@xs4al l.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.co m>
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
9506
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 contains the following three floating-point numbers: 1.0 2.0 3.0
9
6505
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 delta binary is pretty huge in size. I have 1 byte file and 2 bytes file, the delta should be 1 byte but somehow it turns out to be 249 bytes using...
9
11404
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 string. 3) Close the file
9
11339
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 various fields, some of which are two and four byte integers. In VB6 I used the Mid function to pull data out of the buffer so I could decrypt it (do...
2
1743
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 use the built-in control because it's inadequate. In theory I would use a que and feed data as it comes in and then have a parser that extracts...
8
8920
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 GetChunk and AppendChunk to read and write binary data from the OLE Object field. I am using VBA and DAO for this experiment. The OLE Object field...
29
4238
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
1900
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 writes out a temporary file. Then the file is parsed (The file contains HTML data + the actual binary file that was transmitted). As part of the...
6
3376
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: 303530313230303102003035303232303031020030353033323030310200303530343230303102003035303732303031020030353038323030310200 breaking that down: 30353031323030310200 30353032323030310200 30353033323030310200
0
7703
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...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8132
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...
1
7678
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...
0
7982
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...
0
6286
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...
1
5514
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...
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
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...

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.