使用字符串流很容易对一个字符串进行十六进制编码,但是是否可以执行反向操作并使用字符串流解码结果字符串?使用字符串流解码一个十六进制编码的字符串
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
int main()
{
std::string test = "hello, world";
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (unsigned ch : test)
ss << std::setw(2) << int(ch);
std::cout << ss.str() << std::endl;
}
我不想直接移位字节或使用旧的c函数,如scanf系列函数。
我相信你还需要在每对中加上0x。 –
@MaciejStachowski不,前缀对于'std :: stoi'是可选的。 –