View previous topic :: View next topic |
Author |
Message |
eaton
Joined: 02 Oct 2004 Posts: 22 Location: cleveland
|
long pointer into byte data stream |
Posted: Mon Jun 05, 2006 6:25 pm |
|
|
I have a stream of 8 bit values coming in. after capturing these 10 bytes in a buffer I want to assign a long pointer to the 2 bytes of interest and interpret them as a 16 bit value. This works fine except for the fact that the 2 bytes are reversed. i.e 0x3c 0x04 coming in is seen as 0x043c. Is there an easy way to reverse this. What I do now is reverse the 2 bytes in the buffer and assign the long pointer in the normal manner. But it is slow and kind of clunky. I'm using memcpy to move the message from the rx buffer into a temporary holding buffer. Perhaps there is some way to modify memcpy. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 05, 2006 8:52 pm |
|
|
Why not load the incoming data in reverse order? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jun 06, 2006 1:31 am |
|
|
Mark wrote: | Why not load the incoming data in reverse order? | I like this solution. A good example of lateral thinking !
The byte swap problem is a common issue when cummunicating between machines with different endianness (Big Endian v.s.Little Endian).
Maybe you can alter the sender to send the bytes swapped already?
Or use the swap routine below Code: | // Swap two bytes without using a temporary storage variable.
inline void Swap(int *x, int *y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
} |
|
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 06, 2006 6:59 am |
|
|
I have to agree that swapping the bytes on the way in, or first, is the 'best' solution to this. I like Ckielstra's byte swap routine, a real example of 'lateral thinking'. The same basic trick, can though be used to do the byte swap on the input!. If you simply 'XOR' the byte counter with 1, and use this as the index to write into the array, all the alternate bytes will be swapped, and probably just about as efficiently as possible. :-)
Best Wishes |
|
|
Guest
|
|
Posted: Tue Jun 06, 2006 9:43 am |
|
|
Thanks for your help guys. The Byte swap module is pretty slick |
|
|
|