#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    15
    Rep Power
    0

    strint to unsigned int conversion!!


    hi!!
    i want to convert a string to int..
    but i guess the value is bit too long to be stored as int....so i want to store it as unsigned int...but cant figure out how to make this conversion..
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    int inbytes1;
    char string[50]="265695678";

    printf("string %s\n", string);
    inbytes1 = atoi(string);
    printf("int: %d\n", inbytes1);
    }
    regards
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    I'm assuming that you're using a 16-bit compiler like Turbo-C. If this is the case, you can use atol() and convert it to a long value instead.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Because the integer types have a fixed number of bits, they have fixed ranges. The number of bits assigned to an int is system dependent, which means that it could have a different number of bits on different systems. It looks like a short int is fixed at 16 bits and a long int at 32 bits, but I'm not sure that's really the case.

    Since most of our work is with a 16-bit compiler, I'm used to an int being 16 bits long, such that a signed int's range is from 32,768 to 32,767 and an unsigned int is from 0 to 65,535. As you can see, your number is still beyond the range of the unsigned int. A long int (32-bit) ranges +/- 2.14 billion (+/- 2 G) or unsigned from 0 to 4.29 billion (4 G). Your number of 265 million will fit in a long int.

    The long int version of atoi() is atol(), which returns a long int value. I didn't find a atoul, but there is also a family of strtoul(), etc, functions which appear to do the same thing as atoi & atol, but I'm not familiar with them and don't know how they differ.

    Both atoi et al. and strtoi et al. appear to handle overflow the same way (trying to convert a number that exceeds the range of the data type); from the description of atoi:
    RETURN VALUES
    The atoi() function returns the converted integer value.

    If the converted value overflows, LONG_MIN or LONG_MAX is returned (according to the sign of the value) and errno is set to ERANGE. If no character could be converted, zero is returned.
    Read the man/help page for the function you end up using for details.

IMN logo majestic logo threadwatch logo seochat tools logo