Alexandru Toma

Data Type conversions

Discussion created by Alexandru Toma on Jul 8, 2011
Latest reply on Jul 8, 2011 by Alexandru Toma

Is there a way how to convert unsigned long long to 8 bytes (char[] or char*) for saving it to a memory flash?

I'm using codewarrior 68k for embedded version 4.2.7.953  for an MC68332 microcontroller.

 

I've manage to make conversions for unsigned short int, unsigned int, unsigned long.

My solution for short int is:  

byteArray[0]=(char)((shortIntValue >> 8) & 0xFF);byteArray[1]=(char)((shortIntValue & 0xFF ));

My solution for int is:    

byteArray[0]=(char)((intValue >> 24) & 0xFF);byteArray[1]=(char)((intValue >> 16) & 0xFF);byteArray[3]=(char)((intValue >> 8) & 0xFF);byteArray[4]=(char)((intValue & 0xFF ));

My solution for long is:

byteArray[0] = (char)((longInt >> 24) & 0xFF) ;byteArray[1] = (char)((longInt >> 16) & 0xFF) ;byteArray[2] = (char)((longInt >> 8) & 0XFF);byteArray[3] = (char)((longInt & 0XFF));

 For data type long long this method doesn't works because the compiler doesn't knows how to shift long long values.

 

Another question of mine is how to convert this values from short int, int, long , long long to readable ascii.

For all data types less long long values i've manage to use mod (%) and div (/)  to get any char of the number.

 

I don't want to use sprintf, printf or other function, because of the code size.

 

Any idea is welcomed.

 

Thank you in advance.

Outcomes