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

These days I am playing around with the C functions atol (), atof () and atoi () , from a blog post I find a tutorial and apply:

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()

one that returns a float value for a string and atoi()

that returns an integer value.

Now, to see the difference between 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 output will be output ( why?, Is there any solution for converting pure strings to integer?)

atol=0 atof=0 atoi=0

      

The string must contain a numeric value. Now change this program as

char a[]={"007hey"};

      

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

atol=7 atof=7.000000 atoi=7

      

so the functions only took 007 and not the rest ( why? ).

Now consider this

char a[]={"hey007?};

      

The program output will be

atol=0 atof=0.000000 atoi=0

      

So I just want to convert strings to numbers and then back to the same text again. I have played with these functions and as you can see I am getting some really interesting results.

Why is this?

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

EDIT:

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

Also I am wondering if I will be using the same output with the same inputs when I use any of your suggestions?

+2


a source to share


4 answers


The high solution to this problem, given that you added the C ++ tag as well, is to use Boost's lexical_cast .



+6


a source


There is no inconsistency as such:

  • atoi

    analyzes before int

  • atof

    analyzes on float

  • atol

    analyzed for long

  • All three parse a string prefix until it reaches the end, or an invalid character
    • The rest of the line (if any) is ignored

So, I just want to convert my strings to a number and then again to the same text



So the number shouldn't be clear to interpret the string? And how long can a string be and how large can the numbers be?

The string can be decoded as byte[]

. Is that good enough?

Perhaps you need something like public key cryptography ?

+3


a source


You can use strtol()

and strtod()

, which are far superior to atol()

and atof()

, because they allow you to check if the conversion succeeded. Functions ato_()

fail, as you saw when you tried to convert "heyyou"

.

+3


a source


You seem to want to create a bijective mapping between arbitrary character strings and real numbers.

This is not what functions are for atol()

, atoi()

and atof()

- they are meant to convert a subset of strings that represent numbers in base 10 to their corresponding long

, int

or float

value (if possible).

There is no built-in function to create the bijective mapping you are using, especially because you have not actually specified how you want the mapping to work. Of course, such a display function can be written in C. The simplest way is to simply treat the string as a sequence of digits in base-255 number (the 256-digit character value'\0'

cannot be part of a C string) with the N least significant digits representing the length of the string (where N is chosen according to your requirements). Note: if you want to do this with arbitrary length strings, you will need to work with the "Big Integer" library (eg GMP or OpenSSL BigNum) - the longest type in standard C, "long long" cannot be mapped one to one to many C strings that contain strings longer than 8 characters because its guaranteed range only includes 18 446 744 073 709 551 615 unique values.

+2


a source







All Articles