atol(), atof(), atoi() function behaviours, is there a stable way to convert from/to string/integer?

40,466

Solution 1

The high-end solution to this problem, given that you also added a C++ tag, is to use Boost lexical_cast.

Solution 2

There is no inconsistency per se:

  • atoi parses to int
  • atof parses to float
  • atol parses to long
  • All three parses the prefix of a string until it hits the end, or an invalid character
    • The rest of the string (if any) is ignored

So i just want to convert my strings to number and then again to same text

So the number doesn't have to be an intelligble interpretation of the string? And how long can the string be, and how big can the numbers be?

A string can be decoded as a byte[]. Is this good enough?

Perhaps you need something like public key cryptography?

Solution 3

You can use strtol() and strtod(), which are far superior than atol() and atof() because they allow you to test whether the conversion succeeded. The ato_() functions fail silently, as you saw when you tried to convert "heyyou".

Solution 4

It seems like you want to create a bijective mapping between arbitrary character strings and real numbers.

That's not what the atol(), atoi() and atof() functions are for - they're for converting the subset of strings that represent numbers in base 10 into the corresponding long, int or float value (if possible).

There is no built-in function for creating the bijective mapping that you're after - particularly since you haven't actually specified how you'd want the mapping to work. Of course, it is possible to write such a mapping function in C. The simplest way is just to treat the string as a sequence of digits in a base-255 number (the 256th character value, '\0' cannot form part of a C string), with the N least-significant digits representing the string's length (where N is chosen according to your requirements). Note that if you want to do this with strings of arbitrary length, you'll need to work with a "Big Integer" library (like GMP, or OpenSSL's BigNum) - the longest type in standard C, "long long" cannot be mapped one-to-one onto the set of C strings that include strings longer than 8 characters, because its guaranteed range includes only 18,446,744,073,709,551,615 unique values.

Share:
40,466
berkay
Author by

berkay

My track is network security. Mainly, application of statistical methods to solve the problems. Success in life is a matter not so much of talent and opportunity as of concentration and perseverance. C. W. Wendte

Updated on December 08, 2020

Comments

  • berkay
    berkay over 3 years

    In these days I'm playing with the C functions of atol(), atof() and atoi(), from a blog post I find a tutorial and applied:

    Here are my results:

    void main()
    {
        char a[10],b[10];
        puts("Enter the value of a");
        gets(a);
        puts("Enter the value of b");
        gets(b);
        printf("%s+%s=%ld and %s-%s=%ld",a,b,(atol(a)+atol(b)),a,b,(atol(a)-atol(b)));
        getch();
    }
    

    There is atof() which returns the float value of the string and atoi() which returns integer value.

    Now to see the difference between the 3 I checked this code:

    main()
    {
        char a[]={"2545.965"};
        printf("atol=%ld\t atof=%f\t atoi=%d\t\n",atol(a),atof(a),atoi(a));
    }
    

    The output will be

    atol=2545 atof=2545.965000 atoi=2545
    
    char a[]={“heyyou”};
    

    Now when you run the program the following will be the output (why?, is there any solution to convert pure strings to integer?)

    atol=0 atof=0 atoi=0
    

    The string should contain the numeric value. Now modify this program as

    char a[]={“007hey”};
    

    The output in this case (tested in Red Hat Linux) will be

    atol=7 atof=7.000000 atoi=7
    

    so the functions have taken 007 only, not the remaining part (why?).

    Now consider this

    char a[]={“hey007?};
    

    The output of the program will be

    atol=0 atof=0.000000 atoi=0
    

    So I just want to convert my strings to numbers and then again to the same text. I played with these functions, and, as you see, I'm getting really interesting results.

    Why is that?

    Are there any other functions to convert from/to string/integer and vice versa?

    EDIT:

    So as an input, if I take some names, or whatever, I will convert them to integers/floats... then apply some other functions.

    Also I'm curious about if I will take the same output with the same inputs when I use any of your suggestions?