2012-10-21 222 views
0

可能重複:
how do I print an unsigned char as hex in c++ using ostream?
Convert ASCII string into Decimal and Hexadecimal Representations打印出十六進制?

我想用isprint判斷打印出的字符的十六進制值()。但是,我無法讓它工作。這是我的嘗試:

getline(cin, w); 
for(unsigned int c = 0; c < w.size(); c++) 
{ 
    if(!isprint(w[c])) 
    { 
    cout << "ERROR: expected <value> found " << hex << w[c] << endl; 
    return 0; 
    } 
} 

任何人都可以幫我打印出這個十六進制值嗎?謝謝!我輸入的東西,如:

í

,我想這是十六進制值。

+0

什麼是'w',一個std :: string? – vz0

+0

試試這個嗎? http://www.cplusplus.com/reference/iostream/manipulators/hex/ – PRB

回答

3

默認情況下,字符被打印爲字符串字符。嘗試將char轉換爲一個像這樣的常量int:

cout << "ERROR: expected <value> found " << hex << static_cast<int>(w[c]) << endl;