简体   繁体   English

产生十六进制值错误的整数

[英]integer producing hex values error

something weird is happening to my program. 我的程序发生了一些奇怪的事情。 I am currently using lots of threads in my program, and will not be feasible to paste everything here. 我目前在程序中使用很多线程,因此无法在此处粘贴所有内容。

However this is my problem: 但是,这是我的问题:

int value = 1000;
std::cout << value << std::endl;
//output:  3e8

Any idea why is my output 3e8? 知道为什么我的输出是3e8吗?

Whats the command to fix it back to print decimal values? 用什么命令将其修复以打印十进制值?

Thanks in advance! 提前致谢! :) :)

Some other thread changed the default output radix of the std::cout stream to hexadecimal. 其他一些线程将std::cout流的默认输出基数更改为十六进制。 Note that 1000 10 = 3e8 16 , ie 1000 == 0x3e8 . 请注意,1000 10 = 3e8 16 ,即1000 == 0x3e8

Somewhere in your program a call such as: 在程序中的某处调用,例如:

std::cout << std::hex << value;

has been used. 已经用过。 To revert output to normal (decimal) use: 要将输出恢复为普通(十进制),请使用:

std::cout << std::dec;

here is a relevent link to the different ways numbers can be output on std::cout. 这是一个相关联的链接指向在std :: cout上输出数字的不同方式。

Also, as pointed out in the comments below, the standard method of modifying cout flags safely appears to be the following: 另外,如下面的评论所指出,安全修改cout标志的标准方法似乎如下:

ios::fmtflags cout_flag_backup(cout.flags()); // store the current cout flags

cout.flags ( ios::hex ); // change the flags to what you want

cout.flags(cout_flag_backup); // restore cout to its original state

Link to IO base flags 链接到IO基本标志


As stated in the comments below, it would also be wise to point out that when using IO Streams it is a good idea to have some form of synchronisation between the threads and the streams, that is, make sure no two threads can use the same stream at one time. 如下面的评论所述,还应该指出,使用IO流时,最好在线程和流之间进行某种形式的同步,即确保没有两个线程可以使用相同的同步。一次流。
Doing this will probably also centralise your stream calls, meaning that it will be far easier to debug something such as this in the future. 这样做可能还会集中您的流调用,这意味着将来调试诸如此类的东西会容易得多。
Heres an SO question that may help you 这是一个可能帮助您的SO问题

Chances are that another thread has changed the output to hex on cout. 可能是另一个线程将输出更改为十六进制输出。 I doubt that these streams are thread-safe. 我怀疑这些流是否是线程安全的。

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

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