关于cout输出格式 [英] about cout output format

查看:83
本文介绍了关于cout输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。


浮动b;


b = 1.234;


cout<<< hex<< b<< endl;


为什么它不能输出b的十六进制表示?

I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?

推荐答案



" David" <易**** @ gmail.com>在消息中写道

新闻:19 ************************** @ posting.google.c om ...

"David" <yi****@gmail.com> wrote in message
news:19**************************@posting.google.c om...
我有以下代码。

浮动b;

b = 1.234;

cout<< hex< < b<< endl;

为什么它不能输出b的十六进制表示?
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?




据我所知, "十六进制"是一个标志,指定表示的基数

的整数值,而不是浮点值。 (我甚至不确定你怎么用
代表1.234的十六进制???)


-Howard




As far as I can tell, "hex" is a flag specifying the base for representation
of an integer value, not a floating-point value. (I''m not even sure how you
would represent 1.234 in hex???)

-Howard





" David" <易**** @ gmail.com>在消息中写道

新闻:19 ************************** @ posting.google.c om ...

"David" <yi****@gmail.com> wrote in message
news:19**************************@posting.google.c om...
我有以下代码。

浮动b;

b = 1.234;

cout<< hex< < b<< endl;

为什么它不能输出b的十六进制表示?
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?




十六进制格式用于整数值。一个hex

实数什么都不是。如果你想看

来查看类型''float''对象的位模式,

用十六进制表示:


#include< algorithm>

#include< ios>

#include< iostream>

#include< iterator>


int main()

{


float f(1.234f);

unsigned char raw [sizeof f];

*(float *)raw = f;


std :: cout<< std :: hex;


std :: copy(raw,raw + sizeof raw,


std :: cout.put(''\ n'');


返回0 ;

}


但请注意,平台之间的输出会有所不同,

因为并非全部使用相同的浮动表示< br $> b $ b点值。


-Mike



The hex format is for integer values. A "hex
real number" doesn''t mean anything. If you want
to look at the bit pattern of a type ''float'' object,
expressed in hex:

#include <algorithm>
#include <ios>
#include <iostream>
#include <iterator>

int main()
{

float f(1.234f);

unsigned char raw[sizeof f];
*(float*)raw = f;

std::cout << std::hex;

std::copy(raw, raw + sizeof raw,
std::ostream_iterator<unsigned int>(std::cout, ""));

std::cout.put(''\n'');

return 0;
}

But note that the output will vary among platforms,
as not all use the same representation for floating
point values.

-Mike


2004年10月18日星期一22:48:56 comp.lang.c ++中的GMT,Mike Wahler

< mk ****** @ mkwahler.net>写道,
On Mon, 18 Oct 2004 22:48:56 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
十六进制格式用于整数值。 hex
实数什么都不是。
The hex format is for integer values. A "hex
real number" doesn''t mean anything.




当然可以。小数点后的第一个十六进制数字是小数部分中的十六分之一的数字。第二个是

256'的数量,依此类推。它与

十进制格式中的实数完全相同,除了使用十六进制而不是十进制。


十进制1.234 =十六进制1.3BE76C


但是对它没有太大的实际需求,所以没有人愿意在C ++中包含它





Of course it does. The first hex digit after the radix point is the
number of sixteenths in the fractional part. The second is the number of
256''ths, and so on. It means exactly the same thing as a real number in
decimal format, except using base sixteen instead of ten.

Decimal 1.234 = hex 1.3BE76C

But there is not much real demand for it, so nobody bothered to include it
in C++.


这篇关于关于cout输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆