#include <iostream>
#include <stream>
#include <sstream>
std::string StringToHex(const std::string& data)
{
const std::string hex = "0123456789ABCDEF";
std::stringstream ss;
for (std::string::size_type i = 0; i < data.size(); ++i)
ss << hex[(unsigned char)data[i] >> 4] << hex[(unsigned char)data[i] & 0xf];
std::cout << ss.str() << std::endl;
return ss.str();
}
std::string HexToStr(const std::string& str)
{
std::string result;
for (size_t i = 0; i < str.length(); i += 2)
{
std::string byte = str.substr(i, 2);
char chr = (char)(int)strtol(byte.c_str(), NULL, 16);
result.push_back(chr);
}
return result;
}
int main()
{
HexToStr(StringToHex("你好,快来看电影"));
system("pause");
}
string与hex相互转化(C++)
最新推荐文章于 2024-10-05 12:05:06 发布