char to int - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

char to int

adeydasadeydas Posts: 43Member
Hi!
is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.

Thank you.
Abhishek
http://deydas.net

Comments

  • MT2002MT2002 Posts: 1,444Member
    : Hi!
    : is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.
    :
    : Thank you.
    : Abhishek
    : http://deydas.net
    :
    [blue]
    Try [b]strtol[/b] declared in . [b]strtol[/b]
    has the form..

    long strtol(const char * s, char * *endptr, int radix);

    where [b]s[/b] is the string, [b]endptr[/b] is a ptr
    to buffer to store first non-int char, [b]radix[/b] is
    the base of the number to convert it to.

    For example...[/blue][code]
    #include

    [green]// convert string "123" into int[/green]
    int iValue = strtol ("123",0 ,10);[/code][blue]
    Hope this helps;
    ~mt2002[/blue]
  • stoberstober Posts: 9,765Member ✭✭✭
    atoi() is even easier, providing the string is all decimal digits -- no hex or octal.
    [code]
    char str[] = "1234";
    int n = atoi(str);
    [/code]
  • MT2002MT2002 Posts: 1,444Member
    : atoi() is even easier, providing the string is all decimal digits -- no hex or octal.
    : [code]
    : char str[] = "1234";
    : int n = atoi(str);
    : [/code]
    :
    [blue]
    I just want to note that atoi() isnt ANSI compatible,
    however most compilers support it. I heard some GCC
    compilers dont, though (I dont use GCC).[/blue]
  • stoberstober Posts: 9,765Member ✭✭✭
    : [blue]
    : I just want to note that atoi() isnt ANSI compatible,
    : however most compilers support it. I heard some GCC
    : compilers dont, though (I dont use GCC).[/blue]
    :


    yes it is ansi C --

    http://www.cplusplus.com/ref/cstdlib/atoi.html

  • MT2002MT2002 Posts: 1,444Member
    : : [blue]
    : : I just want to note that atoi() isnt ANSI compatible,
    : : however most compilers support it. I heard some GCC
    : : compilers dont, though (I dont use GCC).[/blue]
    : :
    :
    :
    : yes it is ansi C --
    :
    : http://www.cplusplus.com/ref/cstdlib/atoi.html
    [blue]
    Your right--I misread atoi() as itoa()! :-)[/blue]
  • pseudocoderpseudocoder Posts: 691Member ✭✭✭
    : Hi!
    : is there a way to convert value of a string into an integer? i am doing a simultaneous equation program that needs this.
    :
    : Thank you.
    : Abhishek
    : http://deydas.net
    :

    If you're using 0 to fill in missing terms, then sscanf will return the number of conversions instead of a value.

    [code]
    #include
    #include

    int main(void)
    {

    int n;

    printf("hex: %2d
    ", sscanf("FF", "%x", &n) == 1 ? n : -1);
    printf("oct: %2d
    ", sscanf("377", "%o", &n) == 1 ? n : -1);
    printf("dec: %2d
    ", sscanf("255", "%d", &n) == 1 ? n : -1);

    return (0);
    }

    [/code]
    It *should* work with dec, hex, or oct values.
Sign In or Register to comment.