C++ 十六进制字符串到uint8\u t msg[]
我想把字符转换成十六进制字符串C++ 十六进制字符串到uint8\u t msg[],c++,arrays,hex,uint8t,C++,Arrays,Hex,Uint8t,我想把字符转换成十六进制字符串 "0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98" 发送到uint8\u t msg[],但不知道如何执行 这似乎很简单,但一直无法理解。我想将每个字符转换为一个uint8\thex值。例如,如果我有 string result = "0123456789abcdef"; 如何将字符串转换为: uint8_t msg[] = "0123456789abcdef"; 这个函数(
"0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98"
发送到uint8\u t msg[]
,但不知道如何执行
这似乎很简单,但一直无法理解。我想将每个字符转换为一个uint8\t
hex值。例如,如果我有
string result = "0123456789abcdef";
如何将字符串转换为:
uint8_t msg[] = "0123456789abcdef";
这个函数(谢谢)
向量十六进制字节(常量字符串和十六进制){
向量字节;
for(无符号整数i=0;i
使用上面的函数,我们得到字节向量并调用方法data()
我要感谢社区,现在一切正常。谢谢你的评论,我已经绝望了,我不能做这么简单的事情。
特别感谢@johnny mopp编辑-更新为就绪字节而非字符 与使用
.substr()
调用Cstrtol
并强制转换到uint8\t
不同,您可以简单地使用istringstream
和std::setbase(16)
将字节作为无符号的
值直接读取到vector msg
中。看
例如,您可以从包含十六进制字符的字符串中创建一个istringstream
,然后连同uint8\u t
向量和一个临时无符号
,以便在返回向量之前直接读取,例如
std::string result ("0123456789abcdef"); /* input hex string */
std::string s2; /* string for 2-chars */
std::istringstream ss (result); /* stringstream of result */
std::vector<uint8_t> msg; /* vector of uint8_t */
while ((ss >> std::setw(2) >> s2)) { /* read 2-char at a time */
unsigned u; /* tmp unsigned value */
std::istringstream ss2 (s2); /* create 2-char stringstream */
ss2 >> std::setbase(16) >> u; /* convert hex to unsigned */
msg.push_back((uint8_t)u); /* add value as uint8_t */
}
(注意输出中需要的强制转换,以明确告诉cout
将uint8\u t
值视为无符号
值,而不是默认为字符类型的uint8\u t
值
示例使用/输出
$ ./bin/hexstr2uint8_t
string: 0123456789abcdef
msg:
01
23
45
67
89
ab
cd
ef
(注意这次存储了8个uint8\u t
(“字节”)值,而不是16个字符的值)
这只是一个使用C++ IoSokes特性的替代方案,它避免了需要到处乱扔东西,而不是直接调用<代码> StrutoL/Cuff>(这在您的案例中应该是“代码> StrutL</代码>开始”)。 手动十六进制转换
在上一条评论中,您指出使用iostream和stringstream进行转换的速度很慢。您可以尝试通过消除stringstream并使用string::iterator
逐步遍历字符串手动转换每个字符并形成每个uint8\t
字节来优化位(防止最后的半字节或1/2字节),例如
#包括
#包括
#包括
#包括
/*hexchar到value的简单手动转换*/
uint8_t c2hex(常量字符c)
{
uint8_t u=0;
如果Strtol是一个单一的数字,但C++中没有数据类型,它可以容纳一个大的数字,就像你的字符串中的一个。每个字节应该转换为字节还是什么?请澄清。你的问题可能是下面的重复,答案可能不是最有效或C++风格:代码似乎工作正常:。你的问题是什么?这是你要找的函数吗?@vbujym基本问题是这个函数是接受字符数据还是整数数据?也就是说,你传递给函数的字节将如何解释?你实际上没有说(也许你不知道)。它看起来更好,但不幸的是,它不是同一件事。我需要这个函数进行哈希运算,如果您使用您的选项,您会得到不同的结果。我的函数给出了正确的结果,我想它是按字节顺序的,但我无法理解发生了什么。我使用这个简单的库metod picosha2::hash256_hex_string(hash,result)string s=“0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352”向量散列=十六字节;//我的函数result=0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98//它是真的不,你的函数给字符,我的函数给字节,这就是所有的区别。我只是知道我一点都不懂,现在我明白了。这让人困惑,因为每个“字节”(例如“1f”
都是两个“半字节“或两个ASCII字符)。将其更改为读取字节而不是字符同样容易。您所做的只是将std::setw(1)
更改为std::setw(2)
。很抱歉造成混淆。您可以帮我解决这个问题,因为我肯定已经完全弄糊涂了。我将非常感激!setw(2)不,这很简单,不起作用!当然,我刚刚编辑了我的答案。每个十六进制数字0,1,2,3…c,d,e,f都是一个可以用4位表示的值(称为“半字节”)。每个字节有2个半字节。但是,您键入的所有字符都是字符
,因此ASCII字符0、1、2、3…c、d、e、f
由32-126
范围内的7位值表示。要存储为uint8\u t
,必须将每个ASCII字符字节转换为十六进制数字。然后,因为您可以容纳两个十六进制数字在每个字节中,您会看到每个十六进制字节都是01,23,…ef
(这确实让人困惑),请参见
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
int main (void) {
std::string result ("0123456789abcdef"); /* input hex string */
std::string s2; /* string for 2-chars */
std::istringstream ss (result); /* stringstream of result */
std::vector<uint8_t> msg; /* vector of uint8_t */
while ((ss >> std::setw(2) >> s2)) { /* read 2-char at a time */
unsigned u; /* tmp unsigned value */
std::istringstream ss2 (s2); /* create 2-char stringstream */
ss2 >> std::setbase(16) >> u; /* convert hex to unsigned */
msg.push_back((uint8_t)u); /* add value as uint8_t */
}
std::cout << "string: " << result << "\nmsg: \n";
for (auto& h : msg) /* for each element of msg, output hex value */
std::cout << "\t" << std::setfill('0') << std::hex << std::setw(2)
<< (uint32_t)h << '\n';;
}
$ ./bin/hexstr2uint8_t
string: 0123456789abcdef
msg:
01
23
45
67
89
ab
cd
ef
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
/* simple manual conversion of hexchar to value */
uint8_t c2hex (const char c)
{
uint8_t u = 0;
if ('0' <= c && c <= '9')
u = c - '0';
else if ('a' <= c && c <= 'f')
u = c - 'W';
else if ('A' <= c && c <= 'F')
u = c - '7';
else
std::cerr << "error: invalid hex char '" << c << "'\n";
return u;
}
int main (void) {
std::string s ("0123456789abcdef");
std::vector<uint8_t> msg;
for (std::string::iterator n = s.begin(); n != s.end(); n += 2) {
uint8_t u = c2hex (*n); /* save high-nibble */
if (n + 1 != s.end()) /* if low-nibble available */
u = (u << 4) | c2hex (n[1]); /* shift high left 4 & or */
msg.push_back(u); /* store byte in msg */
}
std::cout << "string: " << s << "\nmsg:\n";
for (auto& h : msg)
std::cout << "\t" << std::setfill('0') << std::hex
<< std::setw(2) << (unsigned)h << '\n';
}
uint8_t u = c2hex (n[1]) | (c2hex (*n) << 4);