2014-09-11 54 views

回答

2

是这样的:

std::stringstream ss; 
ss << (char) 0x11; 
std::string str3 = ss.str(); 

有了:

#include <string> 
#include <sstream> 

或者,如果你真的有 “11” 字符串开头:

std::string value = "11"; 
std::stringstream temp; 
temp << std::hex << value; 
char c; 
temp >> c; 
std::stringstream ss; 
ss << c; 
+2

应该是'0x11'。 – timrau 2014-09-11 12:12:12

+0

@timrau:谢谢,修正了这个问题。 – 2014-09-11 12:12:32

+0

@Martijn Courteaux它工作正常。如果你能够用几句话说出第一种方法的作用,我会很高兴。 – idanshmu 2014-09-11 13:01:06

0

如果先有自己的整数低于256那你就可以做

int small = 17; // 0x11 hex 
sanity check here 
str[0] = (char)small; 

如果你开始用绳子

int small = HexToInt("11"); // 0x11 hex, find a HexToInt on the next. 
sanity check here 
str[0] = (char)small;