当我尝试将范围值存储到 8 位的固定宽度整数时,它会显示一些其他值 [ASCII]
when i try to store a ranged value to a fixed width integer of 8 bit it shows me some other value [ASCII]
#include <iostream>
#include <cstdint>
int main() {
std::uint8_t i{5}; // direct initialization
std::cout << i;
return 0;
}
我无法获得值 5,而是获得了其他值。
为什么这段代码给我一些其他的 ASCII 值而不是值 5?
使用
std::cout << static_cast<int>( i );
类型 std::uint8_t 被定义为 unsigned char 的别名。
这是一个演示程序。
#include <iostream>
#include <cstdint>
int main()
{
std::uint8_t i { 65 };
std::cout << i << '\n';
std::cout << static_cast<int>( i ) << '\n';
}
它的输出是
A
65
#include <iostream>
#include <cstdint>
int main() {
std::uint8_t i{5}; // direct initialization
std::cout << i;
return 0;
}
我无法获得值 5,而是获得了其他值。
为什么这段代码给我一些其他的 ASCII 值而不是值 5?
使用
std::cout << static_cast<int>( i );
类型 std::uint8_t 被定义为 unsigned char 的别名。
这是一个演示程序。
#include <iostream>
#include <cstdint>
int main()
{
std::uint8_t i { 65 };
std::cout << i << '\n';
std::cout << static_cast<int>( i ) << '\n';
}
它的输出是
A
65