可以通过stringstream的格式化字符串完成转换:
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
string uintToHexStr(unsigned long data)
{
if(data == 0)
{
return "0x00";
}
string res;
stringstream sstream;
while( data > 0)
{
sstream.str("");
int tmp = data & 0xff;
sstream << "0x" << setw(2) << setfill('0') << hex << tmp;
string &&tmpStr = sstream.str();
if(res.length() == 0)
{
res = tmpStr;
}
else
{
res = tmpStr +