CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

long pointer into byte data stream

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eaton



Joined: 02 Oct 2004
Posts: 22
Location: cleveland

View user's profile Send private message Send e-mail

long pointer into byte data stream
PostPosted: Mon Jun 05, 2006 6:25 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Mon Jun 05, 2006 8:52 pm     Reply with quote

Why not load the incoming data in reverse order?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 06, 2006 1:31 am     Reply with quote

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







PostPosted: Tue Jun 06, 2006 6:59 am     Reply with quote

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








PostPosted: Tue Jun 06, 2006 9:43 am     Reply with quote

Thanks for your help guys. The Byte swap module is pretty slick
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group