How do I split up a long value (32 bits) into four char variables (8bits) using C?

Posted by Jordan S on Stack Overflow See other posts from Stack Overflow or by Jordan S
Published on 2010-04-30T19:46:06Z Indexed on 2010/04/30 20:07 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

I have a 32 bit long variable, CurrentPosition, that I want to split up into 4, 8bit characters. How would I do that most efficiently in C? I am working with an 8bit MCU, 8051 architectecture.

unsigned long CurrentPosition = 7654321;
unsigned char CP1 = 0;
unsigned char CP2 = 0;
unsigned char CP3 = 0;
unsigned char CP4 = 0;
// What do I do next? 

Should I just reference the starting address of CurrentPosition with a pointer and then add 8 two that address four times?

It is little Endian.

ALSO I want CurrentPosition to remain unchanged.

© Stack Overflow or respective owner

Related posts about c-programming

Related posts about long