views:

247

answers:

4

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 numeric value now modify this program as

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

the output in this case(tested in Red hat) will be

atol=7 atof=7.000000 atoi=7

so the functions has 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 number and then again to same text, i played with these functions and as you see i'm getting really interesting results?

why is that?

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 same output with the same inputs when i use any of your suggestions?

+6  A: 

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

Dirk Eddelbuettel
+1 for the C++ answer (given the tag). Reading the code, however, I think a pure C solution may be required.
Johnsyweb
@Dirk Eddelbuettel thanks, i start using it.That's what i'm looking for.
berkay
+2  A: 

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?

polygenelubricants
okey i see that however my results are "zero", how can i build a stable program using these functions?
berkay
@Berkay: do you want an encoding/decoding algorithm, or do you want to parse `"10"` to the `int` value `10`? What would you parse `"xxx"` into, then?
polygenelubricants
@polygenelubricants, my goal is to convert string(only consists characters to integer)
berkay
@Berkay: does the integer have to be fit in 32-bit/64-bit? Because this will not be possible for longer strings.
polygenelubricants
so as an input if i take some names not more than 32 bits. My aim is to convert everything to integers/floats that i take as an input from the users.
berkay
@Berkay: I'm not familiar with C/C++ spec, but each `char` of a string is essentially a number (e.g `'0' == 48` in ASCII encoding), so you can work like this if this is what you want, treating a string as a base-256 number. Such mapping will not work for long strings, though. `"hey you"` is not a 32-bit string. You can use an arbitrary precision integer library to represent longer string as larger numbers, but I'm not sure what you're trying to do.
polygenelubricants
i got it :) thanks for your help +1
berkay
+3  A: 

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".

James McNellis
ok will check it. do they work for strings just containing characters?
berkay
@Berkay: These functions convert numbers, in their string representations, to some numeric type. What behavior do you expect if you give them a string that doesn't start with a number?
James McNellis
@James McNellis i just want to play with numbers. 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 take same output with different inputs with strtol and strtod? thanks for your help.
berkay
@Berkay: But what do you expect the result to be? Do you want integers containing the bit-representations of the characters? Do you want some kind of base conversion applied to the characters in the text (e.g., base64 encoding)?
James McNellis
thanks for your help. i got it.
berkay
+2  A: 

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.

caf
@caf thanks for the answer, your first sentence is describing what i'm looking for... great advice.thanks
berkay