简体   繁体   English

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

[英]cout print hex instead of decimal

has it occurred to anyone that a simple std::cout might 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-> currWorker)-> getEntities()。size()) <<“ worker []的实体target-> currWorker <<“]” << std :: endl;

which would print : 将打印:

_Agent [0x2c6d530] is still among 0x1 entities 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 谢谢

You probably have set std::cout to print hex in prior in the context of your code but forget to reset. 您可能已将std :: cout设置为在代码上下文中预先打印十六进制,但忘记了重置。 For example: 例如:

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. 以十进制形式打印。

Try to find line like this std::cout << std::showbase << std::hex; 尝试找到像这样的行std::cout << std::showbase << std::hex; some where in your code, which sets std::cout to print output in hexadecimal with 0x base indicator prefix. 代码中的某些位置,它将std::cout为以0x基本指示符前缀的十六进制输出输出。 To reset it to show decimal add this line std::cout<<std::dec before the current cout. 要将其重置为显示十进制,请在当前cout之前添加以下行std::cout<<std::dec

You can learn more about c++ io manipulators flags here 您可以在此处了解有关c ++ io机械手标志的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM