C++ cout 十六进制值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ cout hex values?
提问by Greg Hewgill
I want to do:
我想要做:
int a = 255;
cout << a;
and have it show FF in the output, how would I do this?
并让它在输出中显示 FF,我该怎么做?
回答by Greg Hewgill
Use:
用:
#include <iostream>
...
std::cout << std::hex << a;
There are many other options to control the exact formatting of the output number, such as leading zeros and upper/lower case.
还有许多其他选项可以控制输出数字的确切格式,例如前导零和大写/小写。
回答by Beno?t
std::hex
is defined in <ios>
which is included by <iostream>
. But to use things like std::setprecision/std::setw/std::setfill
/etc you have to include <iomanip>
.
std::hex
被定义在<ios>
其中被包含<iostream>
。但是要使用诸如std::setprecision/std::setw/std::setfill
/etc 之类的东西,您必须包含<iomanip>
.
回答by Ashwin Nanjappa
To manipulate the stream to print in hexadecimal use the hex
manipulator:
要操纵流以十六进制打印,请使用hex
操纵器:
cout << hex << a;
By default the hexadecimal characters are output in lowercase. To change it to uppercase use the uppercase
manipulator:
默认情况下,十六进制字符以小写形式输出。要将其更改为大写,请使用uppercase
操纵器:
cout << hex << uppercase << a;
To later change the output back to lowercase, use the nouppercase
manipulator:
稍后要将输出更改回小写,请使用nouppercase
操纵器:
cout << nouppercase << b;
回答by Yoav
If you want to print a single hex number, and then revert back to decimal you can use this:
如果要打印单个十六进制数,然后恢复为十进制数,可以使用以下命令:
std::cout << std::hex << num << std::dec << std::endl;
回答by Daniel Sloof
I understand this isn't what OP asked for, but I still think it is worth to point out how to do it with printf. I almost always prefer using it over std::cout (even with no previous C background).
我知道这不是 OP 所要求的,但我仍然认为值得指出如何使用 printf 来做到这一点。我几乎总是喜欢使用它而不是 std::cout (即使没有以前的 C 背景)。
printf("%.2X", a);
'2' defines the precision, 'X' or 'x' defines case.
'2' 定义精度,'X' 或 'x' 定义大小写。
回答by Gaurav
There are different kinds of flags & masks you can use as well. Please refer http://www.cplusplus.com/reference/iostream/ios_base/setf/for more information.
您也可以使用不同种类的标志和面具。有关更多信息,请参阅http://www.cplusplus.com/reference/iostream/ios_base/setf/。
#include <iostream>
using namespace std;
int main()
{
int num = 255;
cout.setf(ios::hex, ios::basefield);
cout << "Hex: " << num << endl;
cout.unsetf(ios::hex);
cout << "Original format: " << num << endl;
return 0;
}
回答by jtpereyda
std::hex
gets you the hex formatting, but it is a stateful option, meaning you need to save and restore state or it will impact all future output.
std::hex
为您提供十六进制格式,但它是一个有状态的选项,这意味着您需要保存和恢复状态,否则它将影响所有未来的输出。
Naively switching back to std::dec
is only good if that's where the flags were before, which may not be the case, particularly if you're writing a library.
std::dec
如果那是之前标志所在的位置,则天真地切换回是好的,但情况可能并非如此,特别是如果您正在编写一个库。
#include <iostream>
#include <ios>
...
std::ios_base::fmtflags f( cout.flags() ); // save flags state
std::cout << std::hex << a;
cout.flags( f ); // restore flags state
This combines Greg Hewgill's answer and info from another question.
这结合了 Greg Hewgill 的回答和来自另一个问题的信息。
回答by va6un
Use std::uppercase
and std::hex
to format integer variable a
to be displayed in hexadecimal format.
使用std::uppercase
和std::hex
格式化整数变量a
以十六进制格式显示。
#include <iostream>
int main() {
int a = 255;
// Formatting Integer
std::cout << std::uppercase << std::hex << a << std::endl; // Output: FF
std::cout << std::showbase << std::hex << a << std::endl; // Output: 0XFF
std::cout << std::nouppercase << std::showbase << std::hex << a << std::endl; // Output: 0xff
return 0;
}