converting an string to a single variable...

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

converting an string to a single variable...

6,917 次查看
marcoB
Contributor I
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
标签 (1)
0 项奖励
回复
7 回复数

1,287 次查看
Technoman64
Contributor III
This worked in simulation. You need to type cast the char to a float. I also included a function to convert char array to float.
 
float x1,x2,x3,x4,x5;
char array[6] = {6,5,4,3,2,1};
float MyNumber, Number;
 
/* Function to convert array of char to float */
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) {
 x1 = (float)3 * 10000;
 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];
 
 /* Convert using subroutine */
 Number = ArrayToFloat(&array[0], 6);
 
 for(;:smileywink: {} /* wait forever */
}
 
 

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

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

0 项奖励
回复

1,287 次查看
marcoB
Contributor I

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

0 项奖励
回复

1,287 次查看
sjmelnikoff
Contributor III
Mark,
 
If the six numbers are all single digits, you could convert them to an array of ASCII digits (by adding '0' to each one), then using the library function atof() (in stdlib.h) to convert the array directly to a float.
 
The reason your original calculation doesn't work is that, in the absence of any type definition, var is a signed int, which for a 9S12, means that the maximum value is 32767. Any value that attempts to go above this will wrap around to a negative number.
 
One solution to this is declare var (and x1-x5) as unsigned long, and to use casts in each calculation to ensure that values are not truncated. For example:
 
unsigned long x1;
 
x1 = (unsigned long) array[0] * 10000UL;
 
I'd recommend this in preference to float casts, as these processors are not designed for floating points maths, and so the resulting code is likely to be far less efficient.
 
Also, if you aren't already doing so, it is good practise to give an explicit type to every single variable you declare, and to make them unsigned unless you actually need signed values.
 
Hope that helps.
 
Steve Melnikoff
0 项奖励
回复

1,287 次查看
sjmelnikoff
Contributor III

sjmelnikoff wrote:
If the six numbers are all single digits, you could convert them to an array of ASCII digits (by adding '0' to each one), then using the library function atof() (in stdlib.h) to convert the array directly to a float.

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.

0 项奖励
回复

1,287 次查看
marcoB
Contributor I
thanks...i got it all working now. works well.
This array is coming from a GPS device.
I'm filling up an array with ASCII GPS coordinates through SCI.
thanks,
     mark
0 项奖励
回复

1,287 次查看
gravity
Contributor I
What is the type of your array? signed int, signed long, unsigned?
0 项奖励
回复

1,287 次查看
marcoB
Contributor I

char

only decimals in the array

0 项奖励
回复