# Thread: Char To int help

1. ## Char To int help

I've defined a character array of size 16 (strNumber[16]) that I've
moved a double into. So far everything works fine. Now I try to
move element 0 to an integer field, BOOM.

I tried

int i;
i = atoi(strNumber[0]); // failed
sprintf(i,"%c", strNumber[0]); // failed

getting desparate I even tried
strcpy(i,strNumber[0]); and // failed
strcpy(i,atoi(strNumber[0])); // failed

What am I doing wrong ?

2. i = atoi(strNumber); // i think this one will work

3. Even if i = atoi(strNumber) did work I need
an integer value for each element of strNumber.

4. hang on..

> I've defined a character array of size 16 (strNumber[16]) that I've
> moved a double into.

does this mean what i think it does? please explain - you mean something like "534.23" or did you literally overwrite the strNumber[16] with a double-sized floating point variable.

if the format of your string is "243.312" you can just do this:
i = strNumber[element] - '0';
to get the integer value of each digit, but you'll have to check for the decimal point.

if you literally moved a double-sized variable into your strNumber array, i have no idea what you're trying to accomplish

5. I move a float of type double into a fixed char array
strNumber[16] so that the decimal point always shows up in position 13 provided I stop callers from trying to pass me a
number bigger than 15,2 because I zero pad it with sprintf when
I load it into the array. This is just a small module of code that will
accept a number like 3445.81 and return
THREE THOUSAND FOUR HUNDRED FORTY-FIVE AND 81/100
So I need to extract

strnumber[8] into an int = 0
strnumber[9] into an int = 3
strnumber[10] into an int = 4
strnumber[11] into an int = 4
strnumber[12] into an int = 5
strnumber[13] will always be a decimal point
strnumber[14] into an int = 8
strnumber[15] into an int = 1

to subscript my string array or enum what ever I decide to go with.

6. Code:
```strnumber[13] = '\0';
anInt = atoi( strnumber );```
Quzah.

7. I need an individual integer value for each element of strNumber[0-15]

8. Alright.
Code:
```for( x = 0; x < 16; x++ )
anIntArray[x] = x != 13 ? strnumber[x] - '0' : -1;```
Replace the -1 with whatever you want for your decimal point holder. Or just ignore it. Anyway, all you do is take your character value and subtract '0' from it to get the integer value of said character.

Quzah.

9. Ok I can be a little slow some times but thanks that looks like it might work.

Popular pages Recent additions