Convert unsigned int to 2 unsigned char, upper and lower

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Convert unsigned int to 2 unsigned char, upper and lower

Postby Zener » Sun Feb 26, 2012 7:27 pm

In C, is there an easy way to do this? I am hoping yes...
Zener
 
Posts: 1994
Joined: Sat Feb 21, 2009 1:38 am

Re: Convert unsigned int to 2 unsigned char, upper and lower

Postby franklin97355 » Sun Feb 26, 2012 9:34 pm

Try "anding" with 0xf and then shift right 8.
User avatar
franklin97355
 
Posts: 1706
Joined: Mon Apr 21, 2008 1:33 pm

Re: Convert unsigned int to 2 unsigned char, upper and lower

Postby hjohnson » Sun Feb 26, 2012 10:51 pm

This should work.

#define LOW_BYTE(x) = (x & 0xFF) //just the lower byte
#define HIGH_BYTE(x) = (x>>8) & 0xFF //make the higher byte the lower byte, then take just it.

See: http://www.arduino.cc/playground/Code/BitMath
hjohnson
 
Posts: 76
Joined: Sun Dec 20, 2009 8:17 pm

Re: Convert unsigned int to 2 unsigned char, upper and lower

Postby philba » Mon Feb 27, 2012 5:31 pm

The shift and mask thing is pretty much the standard way of doing it. A bit faster in that it does no shifting or masking. (Not a big deal unless it inside a loop).
Code: Select all
int a;
char *alow = (char *)&a;
char *ahigh = (char *) &a+1;

void setup(){
    a = 0x0102;
    Serial.begin(9600);
    Serial.println((int)*ahigh);
    Serial.println((int)*alow);
}
void loop(){}

prints out
1
2
Last edited by philba on Mon Feb 27, 2012 9:57 pm, edited 1 time in total.
philba
 
Posts: 387
Joined: Mon Dec 19, 2011 5:59 pm

Re: Convert unsigned int to 2 unsigned char, upper and lower

Postby Zener » Mon Feb 27, 2012 7:33 pm

Yep, simple. Did this:

Code: Select all
unsigned int address = 0xBBAA;
unsigned char adrhigh = 0;
unsigned char adrlow = 0;

adrlow = address & 0xFF;
adrhigh = (address>>8) & 0xFF;

Works. I just wasn't sure the compiler would like me mixing types like that...

Thanks
Zener
 
Posts: 1994
Joined: Sat Feb 21, 2009 1:38 am


Return to Microcontrollers

Who is online

Users browsing this forum: No registered users and 2 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [108]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[31]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[34]
LCDs & Displays[48]
Components & Parts[70]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[111]
 
Wireless[14]
Cables[62]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]