LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2010, 06:09 AM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
24 bytes of data manipulation


I have 5 byte of data
rop 2 bytes
& por 3 bytes
first 2 byte I declared as uint16_t rop;
remaining 3 bytes i.e. por I cannot declared as uint24_t (doesnt exist)
How should I declare it ,I am sure 24 bytes of datatype is not there.
 
Old 07-12-2010, 09:02 AM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
There are no 24 bit data types available. But you can use the "uint8_t [3]" data type or "uint32_t" data type and ignore the upper eight bits or, if this in a struct declaration use bit fields:
Code:
 typedef struct {
  uint16_t rop;
  uint32_t por : 24;
 } __attribute__((packed)) mydatatype;
 
Old 07-12-2010, 09:21 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by utkarshrawat View Post
I have 5 byte of data
rop 2 bytes
& por 3 bytes
first 2 byte I declared as uint16_t rop;
remaining 3 bytes i.e. por I cannot declared as uint24_t (doesnt exist)
How should I declare it ,I am sure 24 bytes of datatype is not there.
Read about union in "C".
 
Old 07-12-2010, 09:31 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you appear to have your bits and bytes muddled up.
What programming language are you using?
Would arrays of bytes work for your specific needs?
 
Old 07-12-2010, 11:25 PM   #5
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Original Poster
Rep: Reputation: 15
Actually I have a header that consist of 13 bytes.
Code:
struct header
{
self;//5 byte for this char array[5]
por ;// 3 byte  for this still not known
rop ;//2 byte   for this uint16_t
ack; //1 byte  for this uint8_t
id ; //2 byte   for this uint16_t
}
then this header i need to parse & send it to the client side (socket prging)
IMPORTANT NOTE HEADER should be to 13 bytes on receiving side also (client side)

So as per your reply if I do
uint32_t por : 24;
will it take as 3 bytes only ,on receiving end will it take 13 bytes or 14 bytes
because in POR it requires lots of parsing also
 
Old 07-12-2010, 11:57 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Since you are content to use a character array for five bytes why not use a character array for three?
In fact you might what to use a character array for all 13 bytes.
 
Old 07-13-2010, 08:24 AM   #7
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
Sending and receiving bytes is safe method

Just because your program's internal representation is in bytes, uint_16s, and 24-bit values does not mean that the values should be sent across the network in those size pieces. I would insert a function layer between the data structures and the network layer functions to convert all the data to bytes on transmit and convert back to the other data types on receive. That way you know with certainty which bytes are assembled into which bits in the larger data fields. Network order is defined for some larger data types as well as for bytes, but there is no 24-bit data type universally available on all systems.

I recommend against abusing unions for converting data fields from one form to another. K & R explicitly states that the members of a union may only be read from a union in the same type that they were last written. The ordering of the data in the union is explicitly implementation-dependent. That is, if you write a uint_16 to a union and read it out as two uint_8s, there is no guarantee about the mapping of the bytes relative to the bits in the uint_16. This applies similarly to bit fields, because some systems assign bits starting with the most significant bit and others assign them starting with the least significant bit. Since it is possible for some implementation to order the bits or bytes in the opposite order on one system as another implementation on a different system, it will happen sooner or later. This will embed a bug into the code that may not appear for many months or years and will therefore be very difficult to locate and correct. It may not even be detected until after a lot of data has been corrupted. This can lead to a lot of #ifdef on system types which make the code very difficult to read and maintain. This convoluted sort of attempt to fix such a bad idea is frustrating and ugly and best prevented rather than repaired. If you can not guess why I rant about this, send me email and I will send you sanitized excerpts of code where this was done.

On the other hand, converting bytes to larger data types and using flag words to explicitly assign bits in larger words will always work the same way on every system.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Data manipulation Yogiz Linux - Newbie 2 12-11-2007 08:00 PM
why I can only send two bytes data to HID device? wch_zj Programming 0 01-05-2006 12:04 AM
TX bytes vs. httpd bytes ovrload Linux - Networking 3 10-12-2005 04:19 PM
Sending Data Bytes to a terminal jonty_11 Programming 8 03-28-2005 07:55 PM
help with data manipulation SeT Programming 2 10-20-2004 07:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration