2014-09-11 16 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; 
相關問題