使用 C++十六进制和辛
如果你有下面的代码:
cout << hex << 10;
输出是'a',这意味着十进制的10被转换成它的十六进制值。
然而,在下面的代码中......
int n;
cin >> hex >> n;
cout << n << endl;
当输入是12时,输出变成18。任何人都可以解释转换的细节吗?它是如何变成十进制值的?
我对它变成 int 的点感兴趣。如果细分的话,那就是:
(( cin >> hex ) >> n);
这是正确的吗?
If you have the following code:
cout << hex << 10;
The output is 'a', which means the decimal 10 is converted into its hexadecimal value.
However, in the code below...
int n;
cin >> hex >> n;
cout << n << endl;
When input is 12, the output becomes 18. Can anyone explain the details of the conversion? How did it became a decimal value?
I'm interested in the point where it became an int. If broken down, it would be:
(( cin >> hex ) >> n);
Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
十六进制操纵器仅控制如何读取值 - 它始终使用相同的内部二进制表示形式存储。变量无法“记住”它是以十六进制输入的。
The hex manipulator only controls how a value is read - it is always stored using the same internal binary representation. There is no way for a variable to "remember" that it was input in hex.
十六进制的“12”相当于十进制的“18”。当您将“12”放入十六进制
cin
流时,内部值为十进制 18。当您输出到默认为十进制的流时,您会看到十进制值 - “18”。"12" in hex is "18" in decimal. When you put in "12" into a hex
cin
stream, the internal value is 18 decimal. When you output to a stream which is by default decimal, you see the decimal value - "18".它读取 0x12(十六进制值)并将其存储在 n 中,然后以十进制打印。变量只包含值,不包含有关基数的信息(实际上它们将所有内容存储在基数 2 中)。
It reads 0x12 (a hex value) and stores it in n, which you then print in decimal. Variables simply contain values, they do not contain information about the base (actually they store everything in base 2).