• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Convert from 16bit INT to 8bit CHARs

Hey guys,

I am trying to send data to a kestrel autopilot using an ATmega16. Basically, you feed the autopilot serial data written in arbitrary packets that are well-documented. However, there is a checksum value (of type INT) that needs to be sent (as 2 CHARs) in everyone of these packets. My question is how to break an INT into 2 separate CHARs.

For example... say I want to send an INT of value 1451 but needs to be of type CHAR. The hex value of 1451 is 0x05AB. This can be split to 8bit CHAR hex values 0x05 and 0xAB. Yes? Then I could send each of these individually.

I hope this makes sense, and thank you for your help in advance,
 
I would use bitwise ands and right shifts to seperate the halves.

I'm not sure if this code will work, but the solution probably has a similar method. (You'll need to make sure the 2s compliment doesn't mess you up)

char left = theint >> 8;
char right = theint & 0x00FF);

 
Originally posted by: alpha88
I would use bitwise ands and right shifts to seperate the halves.

I'm not sure if this code will work, but the solution probably has a similar method. (You'll need to make sure the 2s compliment doesn't mess you up)

char left = theint >> 8;
char right = theint & 0x00FF);

He will need to cast, but short of that, the solution you showed is fine.
 
Originally posted by: Cooler
You could do a union. That seems to be the cleanest way.

union {
int iCheckSum;
unsigned char CheckSumBytes[2];
};

iCheckSum= 0x05AB;
printf ("Byte 0 0x%2X Byte 1 x%2X \n", CheckSumBytes[0], CheckSumBytes[1]);



Output will show
Byte 0 0x05 Byte 1 0xAB

 
Originally posted by: EagleKeeper
Originally posted by: Cooler
You could do a union. That seems to be the cleanest way.

union {
int iCheckSum;
unsigned char CheckSumBytes[2];
};

iCheckSum= 0x05AB;
printf ("Byte 0 0x%2X Byte 1 x%2X \n", CheckSumBytes[0], CheckSumBytes[1]);



Output will show
Byte 0 0x05 Byte 1 0xAB

I could not have coded it better myself.
 
I'm not sure that the printf will store the Byte 0 and Byte 1 values to the array. Will it do this? Also, how do I make sure that the printf manipulates the correct int value?
 
The printf is only as an example to let you see what has been stored in the array of bytes.

If the data is displayed backward, then you need to adjust the storage logic.
Instead of the left byte going into the even slot, it goes into the odd slot and the right byte goes into the even slot.
This is based on the way chip makers have implimented the micro code.
 
If the data is displayed backward, then you need to adjust the storage logic.
Instead of the left byte going into the even slot, it goes into the odd slot and the right byte goes into the even slot.
This is based on the way chip makers have implimented the micro code.

With no disrespect, the earlier code is better since it makes no endian presumptions. No code with the exception of network code should be endian specific.
 
Originally posted by: bsobel
If the data is displayed backward, then you need to adjust the storage logic.
Instead of the left byte going into the even slot, it goes into the odd slot and the right byte goes into the even slot.
This is based on the way chip makers have implimented the micro code.

With no disrespect, the earlier code is better since it makes no endian presumptions. No code with the exception of network code should be endian specific.

Yes the other one is a more general solution that will work on all platforms. Union Is just quick easy method that works depending on you CPU. It sliped my mind or i would not have sugested it.
 
Back
Top