Convert from 16bit INT to 8bit CHARs

lotharamious

Member
Feb 17, 2006
61
0
0
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,
 

alpha88

Senior member
Dec 29, 2000
877
0
76
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);

 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
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.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
0
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

 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
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.
 

lotharamious

Member
Feb 17, 2006
61
0
0
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?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
0
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.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
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.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |