C++ cout 打印十六进制而不是十进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15892877/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:52:11  来源:igfitidea点击:

cout print hex instead of decimal

c++

提问by rahman

has it occurred to anyone that a simple std::coutmight print a value in hex format when it is supposed to format just a decimal(like an integer)?

有没有人std::cout想过,当一个简单的应该只格式化一个小数(如整数)时,它可能会以十六进制格式打印一个值?

for example, I have a line as :

例如,我有一行:

std::cout << "_Agent [" << target << "] is still among " << ((target->currWorker)->getEntities().size())<< " entities of worker[" << target->currWorker << "]" << std::endl;

std::cout << "_Agent [" << target << "] 仍然在 " << ((target->currWorker)->getEntities().size())<< " 实体的 worker[" <<目标->currWorker << "]" << std::endl;

which would print :

这将打印:

_Agent [0x2c6d530] is still among 0x1entities of worker[0x2c520f0]

_Agent [0x2c6d530] 仍然在worker[0x2c520f0] 的0x1 个实体中

Note:

笔记:

1-the said out put is sometime decimal and some times hex

1-所说的输出有时是十进制,有时是十六进制

2- the behaviour is smae even if I change ((target->currWorker)->getEntities().size())to (int)((target->currWorker)->getEntities().size())

2-即使我((target->currWorker)->getEntities().size())改为(int)((target->currWorker)->getEntities().size())

any hints?

任何提示?

thanks

谢谢

回答by Yang

You probably have set std::cout to print hex in prior in the context of your code but forget to reset. For example:

您可能已经在代码上下文中将 std::cout 设置为预先打印十六进制但忘记重置。例如:

std::cout<<std::hex<<12;
/*blah blah blah*/
std::cout<<12; //this will print in hex form still

so you have to do like the following

所以你必须做如下

std::cout<<std::dec<<12;

to print in decimal form.

以十进制形式打印。

回答by sowrov

Try to find line like this std::cout << std::showbase << std::hex;some where in your code, which sets std::coutto print output in hexadecimal with 0xbase indicator prefix. To reset it to show decimal add this line std::cout<<std::decbefore the current cout.

尝试std::cout << std::showbase << std::hex;在代码中的某处找到这样的行,该行设置std::cout为以十六进制打印输出,带有0x基本指示符前缀。要将其重置为显示十进制std::cout<<std::dec,请在当前 cout 之前添加此行。

You can learn more about c++ io manipulators flags here

您可以在此处了解有关 c++ io 操纵器标志的更多信息