I am having huge problem.

I am having some sort of weird problem when multiplying by 2^15.

My main goal is I want a subroutine that can take an array of 6 variables and concatinate them into 1 single float variable. ex:

My main goal is I want a subroutine that can take an array of 6 variables and concatinate them into 1 single float variable. ex:

if I havearray[6] = {6,5,4,3,2,1};I want to have a double variable equal to "654321"

anyways...the way I tried to do it, is the following:

var = array[0] * 100000 + array[1] * 10000 + array[2] * 1000 + array[3] * 100 + array[4] * 10 + array[5];

This is the only method I know to do this (as I'm a student working on my final year project learning all this!)

With this method, it works sometimes, but not if array[1] is > 3.

With this method, it works sometimes, but not if array[1] is > 3.

When array[1] is > 3 I start getting negative results that are all wrong!

What I figured out is that it has something to do with 2^15 (32768), which is why array[1] can not be > 3. i dont know why. But I did some testing doing the following: (all variables are doubles)

void main(void) {

x1 = 3 * 10000;

x2 = 4 * 10000;x3 = 4 * 1000000;

x4 = 3000 + 12767;x5 = 3000 + 12768;}

when running this, I got the following results:

x1 = 30000 <----this is what is expected

x2 = -25536 <----this is not what I want

x3 = 4000000 <----I am not overflowing because I can multiply larger #'s

x1 = 30000 <----this is what is expected

x2 = -25536 <----this is not what I want

x3 = 4000000 <----I am not overflowing because I can multiply larger #'s

x4 = 32767 <----this is ok...but from here on is where the errors start

x4 = -32768 <----the sign flips, then it starts counting towards 0 as x goes up

I am using a licenced version of codewarrior with mc9s12e128 with 32-bit float variables enabled.

if anyone can see what i'm missing here, it would be greatly appreciated.

Or if anyone knows an alternate method to convert an array to a single variable, that would be great. I also need to do this vice versa, from a single varible split into an array, i've made code for that, but I get the same problem.

if anyone can see what i'm missing here, it would be greatly appreciated.

Or if anyone knows an alternate method to convert an array to a single variable, that would be great. I also need to do this vice versa, from a single varible split into an array, i've made code for that, but I get the same problem.

This problem ONLY occurs when the 2nd (0X0000) is > 3. All other variables work.

thanks if anyone can help,

mark

mark

char

only decimals in the array

float ArrayToFloat(char *ArrayPtr, int NumberOfElements){

/* Local variables */

float Multiplier = 1;

float Number = 0;

int i;

/* Work backwards through array */

for(i = NumberOfElements; i > 0; i--){

/* Add in current digit */

Number += (float)ArrayPtr[i-1] * Multiplier;

/* Next decade */

Multiplier *= 10;

}

/* Return the converted value */

return Number;

}

void main(void) {

x2 = (float)4 * 10000;

x3 = (float)4 * 1000000;

x4 = (float)3000 + 12767;

x5 = (float)3000 + 12768;

MyNumber = (float)array[0] * 100000;

MyNumber += (float)array[1] * 10000;

MyNumber += (float)array[2] * 1000;

MyNumber += (float)array[3] * 100;

MyNumber += (float)array[4] * 10;

MyNumber += (float)array[5];

Number = ArrayToFloat(&array[0], 6);

}

Message Edited by Technoman64 on 04-07-200606:07 AM

Message Edited by Technoman64 on 04-07-200606:08 AM

thanks! The subroutine worked great. but the MyNumber variable didnt.

they look like they same operations too. for array[6] = {6,5,4,3,2,1};

number = 654321 which is great, but MyNumber = 601073.

also x4 and x4 were 15767 and 15768. Seems a little odd, but the subroutine works, so i'll work off that structure. thanks!

mark

Forgot to mention that, if you do this, the last element of the array needs to contain zero (0, not '0'), indicating the end of the string.

Out of interest, where is your input data coming from? Is it a string to start with (as suggested by the title of the thread)?

Steve M.

This array is coming from a GPS device.

I'm filling up an array with ASCII GPS coordinates through SCI.