c how to convert string to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chazzy69
    New Member
    • Sep 2007
    • 196

    c how to convert string to integer

    firstly i have tried typecasting a string to an integer or rather an element of an array which is a string e.g.

    Code:
    string[0] = "2";
    int a = (int)string[0];
    and i know that for c++ you can use atoi which works fine;

    But i need the conversion to work on c, atoi doesn't seem to be working and when i try the typecast conversion it goes from the number 2 to some like 23465???


    Any help is greatly appreciated this completely seperate from any other of my posts just to clarify.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by chazzy69
    firstly i have tried typecasting a string to an integer or rather an element of an array which is a string e.g.

    Code:
    string[0] = "2";
    int a = (int)string[0];
    and i know that for c++ you can use atoi which works fine;

    But i need the conversion to work on c, atoi doesn't seem to be working and when i try the typecast conversion it goes from the number 2 to some like 23465???


    Any help is greatly appreciated this completely seperate from any other of my posts just to clarify.
    Well ofcourse atoi doesn't work when you are trying to call it with string[0] as a parameter.Atoi takes a const char *ptr,adn you are giving him a single char.

    Conversion from char to int is automatic so if you say

    a=string[0];

    and string[0]='2'(which is ASCII value of 50(usually) or '0'+2),a will become 50 or '0'+2.But you need it to be 2,right?
    So what are you going to do?

    There is a way to do it with atoi,but it's rather cumbersome.It's better to stick with this one.

    Comment

    • chazzy69
      New Member
      • Sep 2007
      • 196

      #3
      Ok, if string[0] = "2" and a = string[0]

      i do get a = 50

      So could i possible say then that a = a - 48, but this would only work number is between 0 and 9.

      Any sugestions how to convert ascii value of 2 back to 2 but keeping it as a string.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by chazzy69
        Ok, if string[0] = "2" and a = string[0]

        i do get a = 50

        So could i possible say then that a = a - 48, but this would only work number is between 0 and 9.

        Any sugestions how to convert ascii value of 2 back to 2 but keeping it as a string.
        atof(), atoi() and atol() also work fine in C, they're functions in stdlib. For the
        other way around (number to string) simply use the sprintf() function.

        kind regards,

        Jos

        Comment

        • chazzy69
          New Member
          • Sep 2007
          • 196

          #5
          Sweat it was a case of forgetting to link to the right header file Thanks heaps everyone for their help.

          Comment

          Working...