extracting characters from array in C

Discussion in 'Programmer's Corner' started by Michel_e_r, Oct 8, 2009.

  1. Michel_e_r

    Thread Starter Member

    May 30, 2008
    10
    0
    Hello,

    I'm trying to retrieve characters stored into an array and move them into a variable. For instance:

    char array[]="1,2,3,4";

    to be stored in a char variable so it ends up like this

    value = 1234

    how can I accomplish this?

    thank you.
     
  2. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Zero out value.
    Scan the array. If the character is numeric, multiply value by 10 and add the de-ASCII'd character. Repeat until done. Show us what you have accomplished.

    --Rich
     
  3. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    I'm puzzled here..
    You say you want to put the result in a char variable?

    A single char is generally an unsigned byte, range 0-255 so I can only guess you mean a character string variable, which is actually a character array in C, as you started with?
     
  4. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    The type of value was not defined by the OP, who is trying to convert a series of chars in an array into a number. (How the commas get in there has me puzzled...)

    And do NOT assume what a char is. I have seen as many compilers declare chars as "signed" as "unsigned."

    --Rich
     
  5. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    Rijori;

    Quote: to be stored in a char variable...
    (and I did say 'generally').
     
Loading...