【问题标题】:1 byte integer data type1字节整数数据类型
【发布时间】:2012-11-28 01:57:02
【问题描述】:

我写了以下代码:

 #include <iostream>
 #include <iomanip>
 #include <stdint.h>

 using namespace std;

 int main()
 {
     uint8_t c;

     cin  >> hex >> c;
     cout << dec << c;

     return 0;
 }

但是当我输入c——十六进制表示12——输出也是c。我期待 12。后来我了解到:

uint8_t 通常是unsigned char 的类型定义。所以它实际上将c 读取为ASCII 0x63。

是否有一个 1 字节的整数在执行 I/O 时表现为整数而不是字符?

【问题讨论】:

  • 不,没有,真可惜

标签: c++ char hex int uint8t


【解决方案1】:

我不知道。

您可以使用更广泛的整数类型进行 I/O,并酌情使用范围检查和强制转换。

【讨论】:

    【解决方案2】:

    恐怕我也不知道有什么办法,但是将十六进制数读入整数类型可以如下完成:

    #include <iostream>
    using namespace std;
    
    int main () {
        short c;
        cin >> std::hex >> c;
        cout << c << endl;
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2010-12-31
      • 2018-12-18
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多