View previous topic :: View next topic |
Author |
Message |
kmp84
Joined: 02 Feb 2010 Posts: 354
|
data type conversion |
Posted: Tue Feb 19, 2013 2:56 pm |
|
|
Hi friends!
I've been searching example for convert data to and from like this :
Code: | 0x11,0x23,0x45,0x67 which represent 11234567(dec) to 0xAB6D07 and reverse operation. |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Feb 19, 2013 3:57 pm |
|
|
binary coded decimal? - to decimal
and then decimal to hex
I'm sure you can find plenty examples online...
This is what i got... not exactly what you want...
Code: | int BCD_to_HEX(int bcdnum)
{
high=bcdnum; // initialize both variables with BCD
low=bcdnum;
high&=0XF0; // clear low byte, keep high byte
low&=0X0F; // clear high byte, keep low byte
if(high!=0X00) // since incoming byte is bcd, if highbyte=0 no conversion is nessesary
{
swap(high); // switch lowbyte with high byte
high*=0X0A; // multiply by ten
}
high+=low; // add high and low byte
Return(high); // return high, now containing minutes in hex
} |
Code: | int HEX_to_BCD(int hexnum)
{
if(hexnum>0X09) // if input less than 10 no conversion is nessesary
{
high=hexnum/0X0A; // devide input by 10 keep result as high byte(there is no reminder)
low=(hexnum-(high*0X0A)); // substract highbyte * 10 from input, save as low byte
swap(high); // switch lowbyte with high byte
high|=low; // or together high and low
Return(high); // return byte converted to BCD
}
Return(hexnum); // in case no conversion is nessesary return input
} |
.... my functions dont handle 4 bytes at the time but... it should get you going...
EDIT: inside the compiler HEX is DEC...
is the same as
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Wed Feb 20, 2013 1:50 am |
|
|
I'd make one addendum to this. Inside the chip, _everything_ is binary. Decimal, hex, octal etc., are all converted by the compiler to binary.
Best Wishes |
|
|
|