Mark Berezny

converting an string to a single variable...

Discussion created by Mark Berezny on Apr 6, 2006
Latest reply on Apr 13, 2006 by Mark Berezny
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:
if I have
 array[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.
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
 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.
This problem ONLY occurs when the 2nd (0X0000) is > 3. All other variables work.
thanks if anyone can help,
       mark

Outcomes