[英]Hex character to int in C++
How can I change a hex character, not string, into a numerical value? 如何将十六进制字符而不是字符串更改为数值?
While typing this question, I found many answers on how to convert hex strings to values. 在输入这个问题时,我发现了很多关于如何将十六进制字符串转换为值的答案。 However, none work for chars.
然而,没有人为chars工作。 I remember reading somewhere that this works for strings:
我记得在某处读到这适用于字符串:
std::string mystr = "12345";
unsigned int myval;
std::stringstream(mystr) >> std::hex >> myval;
However, if I do mystr[x]
in a loop, this code will not work. 但是,如果我在循环中执行
mystr[x]
,则此代码将不起作用。 I have tried adding a new line with std::string temp = mystr[x]
and changing std::stringstream(mystr)
to std::stringstream(temp)
, but that's not working either. 我尝试使用
std::string temp = mystr[x]
添加一个新行并将std::stringstream(mystr)
更改为std::stringstream(temp)
,但这也不起作用。
So how should I do this? 那我该怎么做呢? Currently, I'm searching through a string of the hex chars (
"0123456789abcdef".find(mystr[x]);
) and using the index for the value. 目前,我正在搜索一串十六进制字符(
"0123456789abcdef".find(mystr[x]);
)并使用索引作为值。 However, since it searches, it's slow, even if it's only searching through 16 characters. 但是,由于它搜索,它很慢,即使它只搜索16个字符。
int intval = (hexchar >= 'A') ? (hexchar - 'A' + 10) : (hexchar - '0');
The ternary from levis501 can be expanded to: 来自levis501的三元可以扩展到:
int v = (c >= 'A') ? (c >= 'a') ? (c - 'a' + 10) : (c - 'A' + 10) : (c - '0');
But if you want error checking it gets a bit messy: 但如果你想要错误检查,它会有点乱:
int v = (c < '0') ? -1 :
(c <= '9') ? (c - '0') :
(c < 'A') ? v = -1 :
(c <= 'F') ? (c - 'A' + 10) :
(c < 'a') ? v = -1 :
(c <= 'f') ? (c - 'a' + 10) : -1;
It doesn't look much better in if-else blocks: 在if-else块中它看起来不太好:
int v = -1;
if ((c >= '0') && (c <= '9'))
v = (c - '0');
else if ((c >= 'A') && (c <= 'F'))
v = (c - 'A' + 10);
else if ((c >= 'a') && (c <= 'f'))
v = (c - 'a' + 10);
Since I wanted a fast implementation and validation, I went for a lookup table: 由于我想要快速实现和验证,我去了一个查找表:
int ASCIIHexToInt[] =
{
// ASCII
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// 0x80-FF (Omit this if you don't need to check for non-ASCII)
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
};
In this case it's just: 在这种情况下,它只是:
int v = ASCIIHexToInt[c];
if (v < 0)
// Invalid input
Runnable examples are (hopefully) here and here . 可运行的例子(希望) 在这里和这里 。
You already have a solution that works with strings. 您已经有一个适用于字符串的解决方案。 Use it for
char
s too: 也用它作为
char
:
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char val = 'A';
unsigned int myval;
std::stringstream ss;
ss << val;
ss >> std::hex >> myval;
cout << myval << endl;
}
Something like the following? 像下面这样的东西?
//! \return
//! The numeric value of ch, if it is hex, otherwise -1
int
fromHexChar( char ch )
{
static char const hexChars[] = "0123456789ABCDEF";
char const* p = std::find( begin( hexChars ), end( hexChars ),
::tolower( (unsigned char)ch ) );
return p == end( hexChars )
? -1
: p - begin( hexChars );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.