Thread: Char To int help

  1. #1
    coder
    Join Date
    Apr 2004
    Posts
    6

    Question 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 ?

    Please Help.

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i = atoi(strNumber); // i think this one will work
    .sect signature

  3. #3
    coder
    Join Date
    Apr 2004
    Posts
    6
    Even if i = atoi(strNumber) did work I need
    an integer value for each element of strNumber.

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    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
    .sect signature

  5. #5
    coder
    Join Date
    Apr 2004
    Posts
    6
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    strnumber[13] = '\0';
    anInt = atoi( strnumber );
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    coder
    Join Date
    Apr 2004
    Posts
    6
    I need an individual integer value for each element of strNumber[0-15]

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    coder
    Join Date
    Apr 2004
    Posts
    6
    Ok I can be a little slow some times but thanks that looks like it might work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM