Data Type conversions

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Data Type conversions

1,977 Views
sandilau
Contributor II

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.

Labels (1)
0 Kudos
Reply
4 Replies

1,676 Views
scifi
Senior Contributor I

I am not familiar with this compiler. It does sound strange: you are saying that the type long long is present, but you can't perform shifts and divisions on it.

Anyway, copying variable contents into a byte array is easy enough:

memcpy(&byteArray, &longLongVar, sizeof(longLongVar));

Same approach can be used with other types.

Here is one way to convert an integer into a decimal number string without division:

long2str(long l, char *str){    static const long dgt[] =        { 1000000000L, 100000000L, 10000000L, 1000000L,          100000L, 10000L, 1000L, 100L, 10L, 1L };    int i;

    if (l < 0)    {        l = -l;        *str++ = '-';    }    else    {        *str++ = '+';    }    for (i = 0; i < sizeof(dgt) / sizeof(dgt[0]); i++)    {        char c = '0';        while (l >= dgt[i])        {            l -= dgt[i];            c++;        }        *str++ = c;    }}

As you can see, only subtraction and comparison operations are used. Same approach can work with long long, but you'll have to implement your own subtraction and comparison operations if they are not provided by the compiler.

0 Kudos
Reply

1,676 Views
sandilau
Contributor II

Thank you for your support. But i think that codewarrior doesn't know hot to manage 64 bit variables, or i'm not doing something ok.

 

In atachment is presented the linker error.

 

Ps: I know that i have to modify dgt[] variable for longlong data type.

 

 

0 Kudos
Reply

1,676 Views
kef
Specialist I

I'm not familiar with your target, but looking at attached picture it seems that compiler supports long long, but you forgot to add to the project library, containing long long runtime routines. __rt_sub64 must be routine to subtract long long from long long.

Maybe try creating new project using project wizard. I think you should find there some option to enable long long support. Project wizard should add required library then.

0 Kudos
Reply

1,676 Views
sandilau
Contributor II

Yes you have right.

I've read the documentation and  the libraries C_TRk_4i_68000_MSL.a , C_4i_6800_Runtime.a and C_4i_6800_MSL.a do the trick.

 

With these libraries all my code works quite fine.

:smileywink:

Thank you for the support.

 

Thank you.

0 Kudos
Reply