1 字节整数数据类型

1 byte integer data type

本文关键字:数据类型 整数 字节      更新时间:2023-10-16

我写了以下代码:

 #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的typedef。所以它实际上是将c读作 ASCII 0x63。

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

不是我知道的。

可以使用更宽的整数类型执行 I/O,并根据需要使用范围检查和强制转换。

怕我也不知道办法,但是将十六进制数读取为整数类型可以按如下方式完成:

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