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

I have a 32-bit CurrentPosition variable that I want to split into 4, 8 bits. How can I do this most efficiently in C? I work with an 8-bit MCU, architecture 8051.

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 of two such addresses four times?

This is a little Endian.

ALSO I want CurrentPosition not to change.

+7
c long-integer character
source share
6 answers
  CP1 = (CurrentPosition & 0xff000000UL) >> 24; CP2 = (CurrentPosition & 0x00ff0000UL) >> 16; CP3 = (CurrentPosition & 0x0000ff00UL) >> 8; CP4 = (CurrentPosition & 0x000000ffUL) ; 

You can also access bytes through a pointer,

 unsigned char *p = (unsigned char*)&CurrentPosition; //use p[0],p[1],p[2],p[3] to access the bytes. 
+13
source share

I think you should consider using a union:

 union { unsigned long position; unsigned char bytes[4]; } CurrentPosition; CurrentPosition.position = 7654321; 

Now bytes can be obtained as: CurrentPosition.bytes [0], ..., CurrentPosition.bytes [3]

+5
source share

If you use the 8-bit shift of the MCU, the entire 32-bit variable is a bit of work. In this case, it is better to read 4 bytes of CurrentPosition using pointer arithmetic. Listing:

 unsigned char *p = (unsigned char*)&CurrentPosition; 

does not change CurrentPosition, but if you try to write p [0], you will change the least significant byte of CurrentPosition. If you want to make a copy, follow these steps:

 unsigned char *p = (unsigned char*)&CurrentPosition; unsigned char arr[4]; arr[0] = p[0]; arr[1] = p[1]; arr[2] = p[2]; arr[3] = p[3]; 

and work with arr. (If you want the high byte to change the order in these assignments).

If you prefer 4 variables, you can obviously do:

 unsigned char CP1 = p[0]; unsigned char CP2 = p[1]; unsigned char CP3 = p[2]; unsigned char CP4 = p[3]; 
+2
source share
 CP1 = (unsigned char)(CurrentPosition & 0xFF); CurrentPosition >>= 8; CP2 = (unsigned char)(CurrentPosition & 0xFF); ... 
0
source share
 unsigned char *CP = &CurrentPosition; 

Now CPn to your source code is available through CP[n] .

0
source share

I know this was published some time ago. But for those who are still reading this topic: Many people use the approach, sequentially changing the initial value. Why not let the compiler do the work for you. Use union and let you store values ​​in the same place. Define a union consisting of a 32-bit variable (this will be your currentPosition preservation) and a structure consisting of 4 char variables. Or just a simple 8-bit integer array. When you write CurrentPosition for a long variable, it will be stored in the same place where you read 4 char variables. This method is much less time-consuming and does not allow the compiler to do the work, and not waste time and resources.

0
source share

All Articles