C++ atol()、atof()、atoi() 函数行为,是否有一种稳定的方法可以从/到字符串/整数转换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2846501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:15:28  来源:igfitidea点击:

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

c++cfunction

提问by berkay

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:

在这些日子里,我正在使用atol()、atof() 和 atoi()的 C 函数,从一篇博客文章中我找到了一个教程并应用:

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.

atof()哪个返回字符串的浮点值,atoi()哪个返回整数值。

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

现在要查看 3 之间的区别,我检查了此代码:

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

这种情况下的输出(在 Red Hat Linux 中测试)将是

atol=7 atof=7.000000 atoi=7

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

所以函数只取了 007,而不是剩下的部分(为什么?)。

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?

另外,我很好奇在使用您的任何建议时是否会使用相同的输入进行相同的输出?

回答by Dirk Eddelbuettel

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

考虑到您还添加了 C++ 标记,此问题的高端解决方案是使用Boost lexical_cast

回答by polygenelubricants

There is no inconsistency per se:

本身没有不一致之处:

  • atoiparses to int
  • atofparses to float
  • atolparses 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
  • atoi解析为 int
  • atof解析为 float
  • atol解析为 long
  • 所有三个都解析字符串的前缀,直到它到达结尾,或无效字符
    • 字符串的其余部分(如果有)被忽略


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?

字符串可以解码为byte[]. 这够好吗?

Perhaps you need something like public key cryptography?

也许您需要公钥密码学之类的东西?

回答by James McNellis

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

您可以使用strtol()and strtod(),它比atol()and优越得多,atof()因为它们允许您测试转换是否成功。该ato_()功能失败默默,你看到的,当你试图转换"heyyou"

回答by caf

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, intor floatvalue (if possible).

这不是atol(),atoi()atof()函数的用途 - 它们用于将表示基数为 10 的数字的字符串子集转换为相应的long,intfloat值(如果可能)。

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

没有用于创建您所追求的双射映射的内置函数 - 特别是因为您尚未实际指定您希望映射如何工作。当然,用 C 写这样的映射函数可能的。 最简单的方法就是将字符串视为一个以 255 为基数的数字(第 256 个字符值,'\0'不能构成 C 字符串的一部分),其中 N 个最低有效数字代表字符串的长度(其中 N 是根据您的要求选择的)。请注意,如果要使用任意长度的字符串执行此操作,则需要使用“Big Integer”库(如 GMP 或 OpenSSL 的 BigNum)——标准 C 中最长的类型,“long long”无法映射一对一到包含长度超过 8 个字符的字符串的 C 字符串集,因为它的保证范围仅包括 18,446,744,073,709,551,615 个唯一值。