CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

data type conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

data type conversion
PostPosted: Tue Feb 19, 2013 2:56 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 19, 2013 3:57 pm     Reply with quote

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...
Code:
int x = 0xFF;

is the same as
Code:
int x = 255;


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Wed Feb 20, 2013 1:50 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group