【问题标题】:Printing out Hex? [duplicate]打印十六进制? [复制]
【发布时间】:2012-10-21 01:13:20
【问题描述】:

可能重复:
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;
  }
}

谁能帮我打印出这个十六进制值?谢谢!我正在输入如下内容:

我想要它的十六进制值。

【问题讨论】:

标签: c++


【解决方案1】:

默认情况下,char 打印为字符串字符。尝试将 char 转换为这样的一般 int:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2012-12-14
    • 2013-03-31
    • 1970-01-01
    相关资源
    最近更新 更多