使用C ++ hex和cin [英] Using C++ hex and cin
问题描述
如果您有以下代码:
cout< hex<< 10;
输出是'a',这意味着十进制10被转换为十六进制值。 p>
但是,在下面的代码中...
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?
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.
这篇关于使用C ++ hex和cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!