将十六进制字符串转换为无符号整数问题 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15148495/
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-27 19:05:08  来源:igfitidea点击:

Converting hex string to unsigned int issue C++

c++

提问by Julio Garcia

I am currently designing a code and one of the instructions given is to get the first two digits of a hex string and do a desired operation. Everything that is less than A is taken into account. Anything greater than that is not, it just takes the next number. Which messes up the code.

我目前正在设计一个代码,给出的指令之一是获取十六进制字符串的前两位数字并执行所需的操作。所有小于 A 的都被考虑在内。任何大于它的都不是,它只需要下一个数字。这弄乱了代码。

Here is the line which converts it:

这是转换它的行:

int x = atoi(hex.c_str);

What am I doing wrong?

我究竟做错了什么?

回答by Tuxdude

I'm assuming this is what you meant - you'd like to convert only the first two hexadecimal digits of a string to an unsigned integer. I'm also assuming that the string contains only valid hexadecimal digits. If you want to validate the string, you need to write some extra code for that.

我假设这就是您的意思 - 您只想将字符串的前两个十六进制数字转换为无符号整数。我还假设该字符串仅包含有效的十六进制数字。如果要验证字符串,则需要为此编写一些额外的代码。

Use strtoulto convert a hex string to unsigned integer instead. Also use substr()method of the string class to extract only the initial 2 digits of the hexadecimal string.

使用strtoul一个十六进制字符串到无符号整数转换代替。同样使用substr()string类的方法只提取十六进制字符串的前2位数字。

#include <string>
#include <cstdio>
#include <cstdlib>

int main()
{
    std::string myhex = "abcdef";
    unsigned int x = strtoul(myhex.substr(0, 2).c_str(), NULL, 16);
    printf("x = %d\n", x);
}

This would be your output (i.e. 0xab = 171):

这将是您的输出(即0xab = 171):

x = 171

回答by paddy

int x = strtol(hex.c_str(), NULL, 16);

You can also use sscanfif you want to read a partial string. You mentioned you might want to just take the first two characters:

sscanf如果要读取部分字符串,也可以使用。您提到您可能只想使用前两个字符:

int x;
if( 1 == sscanf(hex.c_str(), "%2x", &x) ) {
    printf( "Value is: %d\n", x );
} else {
    printf( "Conversion failed\n" );
}

Note the above isn't really C++. You can use the std::hexstream modifier for that (note that I called your string mystrthis time to avoid confusion (particularly if you've imported the namespace std):

请注意,以上并不是真正的 C++。您可以std::hex为此使用流修饰符(请注意,我mystr这次调用了您的字符串以避免混淆(特别是如果您导入了 namespace std):

int x;
std::istringstream iss(mystr);
bool ok = (iss >> std::hex >> x);


[edit] I notice in your comment you ask about converting to an unsignedinteger. In that case, why are you not explicitly declaring your integer as unsigned int?

[编辑] 我注意到在您的评论中您询问转换为无符号整数。在这种情况下,您为什么不明确声明您的整数为unsigned int

For unsigned, you should use strtoul.

对于未签名,您应该使用strtoul.

回答by Cheers and hth. - Alf

the atoifunction requires base 10

atoi函数需要以 10 为基数

instead use e.g. strtol

而是使用例如 strtol

it is generally a good idea to read the documentation. just write the function's name into AltaVista(or, for that matter, google). and it will find some documentation for you

阅读文档通常是个好主意。只需将函数的名称写入AltaVista(或者,就此而言,谷歌)。它会为您找到一些文档

回答by Mats Petersson

If you are converting to an unsigned, you probably want to use strtoul- either with a prefix of 0xor giving a base of 16. It is possible that strtolalso works correctly, but that is supposed to give a signed value, so at the very least you may get warnings for mixing signed and unsigned from some compilers.

如果要转换为无符号,你可能想使用strtoul-无论是与前缀0x或给予的16基地有可能strtol也工作正常,但应该给出一个符号值,所以至少是你可能会收到一些编译器混合有符号和无符号的警告。