473,396 Members | 1,975 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,396 software developers and data experts.

Reading binary packet

I am reading a binary packet : 32, 8, 8, 2, 1, 1, 4, 128
I am using the following structure to parse the data:
struct header {
unsigned int a:32;
unsigned int b:8;
unsigned int c:8;
unsigned int d:2;
unsigned int e:1;
unsigned int f:1;
unsigned int g:4;
unsigned int h[4];
};
struct header head;
fread (&head, sizeof head, fp);

My data is getting garbled due to byte ordering differences. The bit
fieds are giving me incorrect values.
I suppose that I will have to use/create some byte ordering routines
( htonl / ntohl ).
I tried reading the data in a char array and then coercing it into
structure pointers so as to avoid multi-byte values but obviously the
data is dependent on how it is written and not how it is read.
Is my approach right or is their a better way to do it?
Jun 27 '08 #1
6 3773
On May 28, 8:40 am, rahul <rahulsin...@gmail.comwrote:
I am reading a binary packet : 32, 8, 8, 2, 1, 1, 4, 128
I am using the following structure to parse the data:
struct header {
unsigned int a:32;
unsigned int b:8;
unsigned int c:8;
unsigned int d:2;
unsigned int e:1;
unsigned int f:1;
unsigned int g:4;
unsigned int h[4];};

struct header head;
fread (&head, sizeof head, fp);

My data is getting garbled due to byte ordering differences. The bit
fieds are giving me incorrect values.
If that struct was written to the file previously with a call to
fwrite(), it should give you the correct result.
Jun 27 '08 #2
On 28 May 2008 at 5:40, rahul wrote:
I am reading a binary packet : 32, 8, 8, 2, 1, 1, 4, 128
I am using the following structure to parse the data:
struct header {
unsigned int a:32;
unsigned int b:8;
unsigned int c:8;
unsigned int d:2;
unsigned int e:1;
unsigned int f:1;
unsigned int g:4;
unsigned int h[4];
};
struct header head;
fread (&head, sizeof head, fp);
fread takes 4 arguments: you need fread (&head, sizeof head, 1, fp);

You asked in another thread about packing. If you don't tell your
compiler to pack this struct then it's very likely to include one byte
of padding between g and h. Use __attribute__((packed)) in gcc.
My data is getting garbled due to byte ordering differences. The bit
fieds are giving me incorrect values.
It's likely that your compiler is ordering the byte containing d,e,f,g
like this:

[ . . . . . . . . ]
_______ _ _ ___
g f e d

So you may need to reorder the bit-fields in your struct.

If you also need to swap the endianness of your 32-bit fields, you can
use a macro like this:

#define CHANGE_END(x) (((x >>24) & 0x000000ff) | ((x >8) & 0x0000ff00) \
| ((x << 8) & 0x00ff0000) | ((x <<24) & 0xff000000) )

Jun 27 '08 #3
rahul <ra*********@gmail.comwrites:
[...]
I had to drop the bit fields as the bits are read into a memory
address and '&' operator is not applicable for bit fields.
I initialized all the members as unsigned char. As the standard goes,
a char is always 1 byte.
Yes, a char is always 1 byte, but a byte is not always 8 bits.

But you're probably safe in assuming that it is on most platforms.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4
FYI
If anyone is looking for a bit library or is just curious about the
implementation, I got it from here
http://michael.dipperstein.com/bitlibs/
This library has 2 small bugs. You need to 0 the memory area before
reading where the bits are to be read
or else you will be getting incorrect results. Secondly, it does an
unnecessary right shift while reading
multiple bits.

It takes care of the endian-ness. I am posting it as I think some of
you might want to comment on the
implementation.
Jun 27 '08 #5
On 28 May, 10:01, Antoninus Twink <nos...@nospam.invalidwrote:
On 28 May 2008 at *5:40, rahul wrote:
I am reading a binary packet : *32, 8, 8, 2, *1, 1, 4, 128
I am using the following structure to parse the data:
struct header {
* *unsigned int a:32;
* *unsigned int b:8;
* *unsigned int c:8;
* *unsigned int d:2;
* *unsigned int e:1;
* *unsigned int f:1;
* *unsigned int g:4;
* *unsigned int h[4];
};
struct header head;
<snip>
You asked in another thread about packing. If you don't tell your
compiler to pack this struct then it's very likely to include one byte
of padding between g and h. Use __attribute__((packed)) in gcc.
it is best to avoid non-portable constructs like this. Other
suggestions
of bit stream libraries etc. are a better way to go

<snip>
--
Nick Keighley
Jun 27 '08 #6
rahul <ra*********@gmail.comwrites:
FYI
If anyone is looking for a bit library or is just curious about the
implementation, I got it from here
http://michael.dipperstein.com/bitlibs/
This library has 2 small bugs. You need to 0 the memory area before
reading where the bits are to be read
or else you will be getting incorrect results. Secondly, it does an
unnecessary right shift while reading
multiple bits.

It takes care of the endian-ness. I am posting it as I think some of
you might want to comment on the
implementation.
It does things in what is, to me, a slightly odd way. The only places
where endianness matters (for this library at least) is when reading
integer values larges than one byte. The author has chosen to do this
by exposing the endianness and using explicit multi-byte arrays. It
is much simpler (usually) to do arithmetic on the desired result type.
That method is fully portable whereas the method used by this library
works only on systems that have been catered for by the programmer (he
considers only two orders). I have written C on a machine where this
would break but of course such machines are rare nowadays, probably
because so much C code breaks on them.

--
Ben.
Jun 27 '08 #7

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

Similar topics

1
by: coder_1024 | last post by:
I'm trying to send a packet of binary data to a UDP server. If I send a text string, it works fine. If I attempt to send binary data, it sends a UDP packet with 0 bytes of data (just the...
4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
2
by: Dave | last post by:
Hi, This is doubtless a really dumb question but is there an elegant way of reading numbers formatted as hex from a datgram? I get a datgram which contains values like this: ---DATAGRAM---...
1
by: richardd | last post by:
Hi, I am writing code to deal with PCAP files. I have a PCAP dump and I am looking at the timestamps in the PCAP packet headers to see if they are in the correct order in the file. To do this I...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
6
by: Klaus Jensen | last post by:
Hi I have some binary files (jpeg), which contain a lot of image-data - and some embedded XML (XMP actually). If I view the file in a hex-editor, there is a lot of binary data - and then in...
9
by: thorley | last post by:
Greetings, since there was no reponse to my previous post about an existing FastCGI server in python, I've taken to writing my own. (which of course I'll share--*if* there's something to share ;) ...
2
by: Damfino | last post by:
Hi all, Newbie question here wrt defining a class that will work on bits read from a binary file. How would you go about doing it? As an example please look at the structure of my data given...
3
by: Andrew Lentvorski | last post by:
Basically, I'd like to use the ctypes module as a much more descriptive "struct" module. Is there a way to take a ctypes.Structure-based class and convert it to/from a binary string? Thanks,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.