如何使用 cout 打印 0x0a 而不是 0xa?
如何使用 cout 打印 0x0a,而不是 0xa?
#include <iostream>
using std::cout;
using std::endl;
using std::hex;
int main()
{
cout << hex << showbase << 10 << endl;
}
How can I print 0x0a, instead of 0xa using cout?
#include <iostream>
using std::cout;
using std::endl;
using std::hex;
int main()
{
cout << hex << showbase << 10 << endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这对 GCC 中的我有用:
如果您厌倦了 iostream 的格式怪异,请给出
更新(2019)
查看已接受的{fmt}库进入 C++20。基准测试显示它比 Boost.Format 更快。
This works for me in GCC:
If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers, yet it is type-safe.
UPDATE (2019)
Check out the {fmt} library that's been accepted into C++20. Benchmarks show it to be faster than Boost.Format.
使用 setw 和 setfill 来自 iomanip
就我个人而言,iostreams 的状态特性总是让我烦恼。我认为升压格式是更好的选择,所以我推荐了另一个答案。
Use setw and setfill from iomanip
Personally, the stateful nature of iostreams always annoys me. I think boost format is a better option, so I'd recommended the other answer.
如果您想以更简单的方式输出十六进制数字,您可以编写如下函数:
更新版本如下;有两种方法可以插入
0x
基本指示器,脚注详细说明了它们之间的差异。原始版本保留在答案的底部,以免给使用它的任何人带来不便。请注意,更新版本和原始版本可能都需要针对字节大小为9 位的倍数。
它可以按如下方式使用:
实时查看这两个选项(详见下面的脚注): 这里。
脚注:
该行负责显示基础,可以是以下任意一种:
<前><代码><< “0x”
<< std::展示库
对于尝试将负十六进制数字输出为
-0x##
而不是<0x##>< 的自定义类型,第一个选项将显示不正确。 /code>,符号显示在基数之后(如
0x-##
)而不是之前。这很少是一个问题,所以我个人更喜欢这个选项。如果这是一个问题,那么在使用这些类型时,您可以在输出基数之前检查是否为负数,然后使用
abs()
(或 自定义abs()
返回无符号值,如果您需要能够处理最大负值2 的补码系统)在val
上。第二个选项将在
val == 0
时省略基数,显示(例如,对于int
,其中int
是32 位)0000000000
而不是预期的0x00000000
。这是因为showbase
标志在内部被视为printf()
的#
修饰符。如果这是一个问题,您可以检查是否
val == 0
,并在出现问题时应用特殊处理。取决于选择哪个选项来显示基础,需要更改两行。
<< "0x"
,那么HEX_BASE_CHARS
是不必要的,可以省略。如果使用
<< std::showbase
,那么提供给setw()
的值需要考虑到这一点:<前><代码><< std::setw((sizeof(T) * CHAR_BIT / HEX_DIGIT_BITS) + HEX_BASE_CHARS)
原始版本如下:
然后可以像这样使用:
工作示例: 此处。
If you want to make an easier way to output a hex number, you could write a function like this:
Updated version is presented below; there are two ways the
0x
base indicator can be inserted, with footnotes detailing the differences between them. The original version is preserved at the bottom of the answer, so as not to inconvenience anyone that was using it.Note that both the updated and original versions may need some tailoring for systems where the byte size is a multiple of 9 bits.
It can be used as follows:
See both options (as detailed in footnotes, below) live: here.
Footnotes:
This line is responsible for showing the base, and can be either of the following:
The first option will display improperly for custom types that try to output negative hex numbers as
-0x##
instead of as<complement of 0x##>
, with the sign displaying after the base (as0x-##
) instead of before it. This is very rarely an issue, so I personally prefer this option.If this is an issue, then when using these types, you can check for negativity before outputting the base, then using
abs()
(or a customabs()
that returns an unsigned value, if you need to be able to handle the most-negative values on a 2's complement system) onval
.The second option will omit the base when
val == 0
, displaying (e.g., forint
, whereint
is 32 bits)0000000000
instead of the expected0x00000000
. This is due to theshowbase
flag being treated likeprintf()
's#
modifier internally.If this is an issue, you can check whether
val == 0
, and apply special handling when it does.Depending on which option was chosen for showing the base, two lines will need to be changed.
<< "0x"
, thenHEX_BASE_CHARS
is unnecessary, and can be omitted.If using
<< std::showbase
, then the value supplied tosetw()
needs to account for this:The original version is as follows:
Which can then be used like this:
Working example: here.
在 C++20 中,您将能够使用
std::format
来执行此操作:
输出:
同时您可以使用 {fmt} 库 ,
std::format
基于。 {fmt} 还提供了print
功能,使这变得更加简单和高效(godbolt):免责声明:我是 {fmt} 和 C++20
std::format
的作者。In C++20 you'll be able to use
std::format
to do this:Output:
In the meantime you can use the {fmt} library,
std::format
is based on. {fmt} also provides theprint
function that makes this even easier and more efficient (godbolt):Disclaimer: I'm the author of {fmt} and C++20
std::format
.答案缺失的重要一点是您必须将
right
与所有上述标志一起使用:The important thing that the answer is missing is that you must use
right
with all of the above mentioned flags:试试这个..您只需根据幅度预先添加零即可。
您可以轻松修改它以处理更大的数字。
因数为 16(一位十六进制数字):
16, 256, 4096, 65536, 1048576, ..
各自
0x10, 0x100, 0x1000, 0x10000, 0x100000, ..
因此你也可以这样写..
等等..:P
try this.. you simply prepend zeroes based on magnitude.
You can easily modify this to work with larger numbers.
Factor is 16 (for one hex-digit):
16, 256, 4096, 65536, 1048576, ..
respective
0x10, 0x100, 0x1000, 0x10000, 0x100000, ..
Therefore you could also write like this..
And so on.. :P
为了缩短输出十六进制的时间,我做了一个简单的
宏
To shorten things up for outputting hex, I made a simple macro
then
使用自动填充“0”或设置将任何数字打印为十六进制。模板允许任何数据类型(例如 uint8_t)
示例:
输出:
Print any number to hex with auto-padding '0' or set. Template allows any data type (e.g. uint8_t)
Example:
Output:
也许不完全是他想要的,但我通常使用类似的东西:
这将在调用它的任何对象中输出每个字节的两位数字,并且不使用(或考虑)任何格式标志 - 我通常使用它来调试,因此将其插入到流中,而不知道当前的格式标志,并且非常强烈地希望不更改它们。
例如,可以轻松修改它以不插入空格;也可以对其进行修改以考虑诸如 std::ios_base::showbase 或 std::ios_base::uppercase 之类的内容。
如果您将其用于调试以外的其他用途,您可能需要将其命名为
AsHex
,而不是HexDump
。Perhaps not exactly what he's looking for, but I usually use something like:
This will output two digits per byte in whatever object it is called on, and doesn't use (or consider) any formatting flags -- I usually use this for debugging, and so insert it into streams with no knowledge of the current format flags, and a very strong desire to not change them.
It can easily be modified to not insert spaces, for example; it would also be possible to modify it to take into consideration things like
std::ios_base::showbase
orstd::ios_base::uppercase
.If you're using it for something other than debugging, you might want to call it something like
AsHex
, rather thanHexDump
.