473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

about cout output format

I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?
Jul 22 '05 #1
16 6503

"David" <yi****@gmail.com> wrote in message
news:19**************************@posting.google.c om...
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?


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


Jul 22 '05 #2

"David" <yi****@gmail.com> wrote in message
news:19**************************@posting.google.c om...
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?


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
Jul 22 '05 #3
On Mon, 18 Oct 2004 22:48:56 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
The hex format is for integer values. A "hex
real number" doesn't mean anything.


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++.

Jul 22 '05 #4

"David Harmon" <so****@netcom.com> wrote in message
news:41***************@news.west.earthlink.net...
On Mon, 18 Oct 2004 22:48:56 GMT in comp.lang.c++, "Mike Wahler"
<mk******@mkwahler.net> wrote,
The hex format is for integer values. A "hex
real number" doesn't mean anything.


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++.


I think its more likely that the OP wants to see the internal representation
of a real number rather than the value. But if he clarifies his requirements
I'm sure he'll get an answer either way.

john
Jul 22 '05 #5
On Mon, 18 Oct 2004 21:22:58 GMT, "Howard" <al*****@hotmail.com>
wrote:

"David" <yi****@gmail.com> wrote in message
news:19**************************@posting.google. com...
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?


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

<mode = nerd>
Hex is base 16 so each position is 1/16th of the position to its left.
Thus after the 'heximal point' the positions are worth 1/16, 1/256,
1/4096 etc.

so 1.234 = 1 + 3/16 + 11/256 + 14/4096 + ...

The sequence 1, 3, 11, 14, ... gives the hex.

This is 1.3BE... in hex.
<mode = what passes for normal>

rossum

--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #6
Thanks for all your inputs. What I really need to know is the internal
bit pattern of the float number (hex presentations). All of your
answers are very helpful!!

Yi
Jul 22 '05 #7
I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'
#include <iostream>
#include <algorithm>
int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}
Jul 22 '05 #8
David wrote:

I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'

#include <iostream>
#include <algorithm>

int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}


It would not have worked anyway.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9
David wrote:

I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'

#include <iostream>
#include <algorithm>

int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}


It would not have worked anyway in the way you want it to work.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

"David" <yi****@gmail.com> skrev i en meddelelse
news:19**************************@posting.google.c om...
I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'
#include <iostream>
#include <algorithm>
int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}


Sorry - it should have been

char const *raw = reinterpret_cast<char const*>(&f);

Note the ampersand.

/Peter
Jul 22 '05 #11

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:41***************@gascad.at...
David wrote:

I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'

#include <iostream>
#include <algorithm>

int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}


It would not have worked anyway.

--
Karl Heinz Buchegger
kb******@gascad.at


What do you mean by that "anyway", anyway?

/Peter
Jul 22 '05 #12
Peter Koch Larsen wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:41***************@gascad.at...
David wrote:

I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'

#include <iostream>
#include <algorithm>

int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}


It would not have worked anyway.

--
Karl Heinz Buchegger
kb******@gascad.at


What do you mean by that "anyway", anyway?


What do you expect to be output?

You cant change the rules. If in

cout << something

'something' is a pointer to characters (const or not), the stream
formatting function expects this to be a real 'string' (that is:
printable characters terminated with '\0'). Just by casting some
pointer to a character pointer, you don't get magically a string
representation.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #13

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:41***************@gascad.at...
Peter Koch Larsen wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:41***************@gascad.at...
> David wrote:
>>
>> I got the following error using perter's method on following code:
>>
>> invalid reinterpret_cast from type `float' to type `const
>> char *'
>>
>> #include <iostream>
>> #include <algorithm>
>>
>> int main(){
>>
>> float f = 1.234;
>>
>> char const *raw = reinterpret_cast<char const*>(f);
>> cout<<raw<<endl;
>>
>> }
>
> It would not have worked anyway.
>
> --
> Karl Heinz Buchegger
> kb******@gascad.at


What do you mean by that "anyway", anyway?


What do you expect to be output?

You cant change the rules. If in

cout << something

'something' is a pointer to characters (const or not), the stream
formatting function expects this to be a real 'string' (that is:
printable characters terminated with '\0'). Just by casting some
pointer to a character pointer, you don't get magically a string
representation.

--
Karl Heinz Buchegger
kb******@gascad.at


Ahh.. I see. I never intended the output of the binary format to be
std::cout << charptr. I only devised a way to get access to that raw data
(where i forgot the ampersand in the first post).

/Peter
Jul 22 '05 #14
Peter Koch Larsen wrote:

Ahh.. I see. I never intended the output of the binary format to be
std::cout << charptr. I only devised a way to get access to that raw data
(where i forgot the ampersand in the first post).


Got it.

It's just that exactly this question or a variation of it

"I have a xxxx and I want to output it, but the compiler won't
let me do it. Thus I casted to char* but I get only garbage.
Why? I thought char* represents a string?"

is asked *very* often. I guess, it was my 'optical code pattern matcher'
interfering when I saw the code snippet :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #15
yi****@gmail.com (David) wrote in message news:<19**************************@posting.google. com>...
Thanks for all your inputs. What I really need to know is the internal
bit pattern of the float number (hex presentations). All of your
answers are very helpful!!

Yi


typedef float myfloat; // try with double

static const int words = sizeof(myfloat)/sizeof(unsigned long);

union float_bits // alignment issues?
{
myfloat f;
unsigned long w[words];
};

int main()
{
cout << "words = " << words << endl;

myfloat f = 1.234;

float_bits fb;
for (int n=0;n<words;++n) fb.w[n] = 0; //not sure if we need this...
fb.f = f;

cout << "hex " << f << " =";
for (int n=0;n<words;++n) cout << ' ' << hex << fb.w[n];
cout << endl;
}

outputs:

words = 1
hex 1.234 = 3f9df3b6

on my machine. If we "typedef double myfloat" output is:

words = 2
hex 1.234 = c8b43958 3ff3be76

This may or may not be what you want.

Regards,

--
Lionel B
Jul 22 '05 #16
Lionel B wrote:
yi****@gmail.com (David) wrote in message news:<19**************************@posting.google. com>...
Thanks for all your inputs. What I really need to know is the internal bit pattern of the float number (hex presentations). All of your
answers are very helpful!!

Yi


typedef float myfloat; // try with double

static const int words = sizeof(myfloat)/sizeof(unsigned long);

union float_bits // alignment issues?
{
myfloat f;
unsigned long w[words];
};

int main()
{
cout << "words = " << words << endl;

myfloat f = 1.234;

float_bits fb;
for (int n=0;n<words;++n) fb.w[n] = 0; //not sure if we need

this... fb.f = f;

cout << "hex " << f << " =";
for (int n=0;n<words;++n) cout << ' ' << hex << fb.w[n];
Ooerr..., make that:

for (int n=words-1;n>=0;--n) cout << ' ' << hex << fb.w[n];

We (is this Western-centric?) tend to read digits left -> right = high
-> low :-/
cout << endl;
}

outputs:

words = 1
hex 1.234 = 3f9df3b6

on my machine. If we "typedef double myfloat" output is:

words = 2
hex 1.234 = c8b43958 3ff3be76

Make that:

hex 1.234 = 3ff3be76 c8b43958

Cheers,

--
Lionel B

Jul 22 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
12
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output...
3
by: Eric Lilja | last post by:
Hi, I need a function that expects a const std::string& containing the visual representation of a number in binary format. It should then output this number in groups of four bits separated by a...
5
by: Mastupristi | last post by:
I want to obtain the c++ equivalent of: unsigned short us = 347; printf("0x%04hX",us); that outputs "0x015B" I ried with: cout.setf(ios_base::hex,ios_base::basefield);
2
by: Generic Usenet Account | last post by:
What exactly is the difference between the hex manipulator and the following statement: cout.setf(ios_base::hex)? According to Stroustrup, Third Edition, Section 21.4.4, "once set, a base is...
3
by: Yudan Yi \(OSU\) | last post by:
I have a question to define a friend operator<< for a class. for example, I can define friend ostream& operator<<(ostream& os, const TTest& x) { ...; return (os); }; While I want to add more...
7
by: Yuanfei | last post by:
Hi There, I just found that there is a problem in vc2005 regarding to time_t and localtime. See code snippets belows. Using this code segment, I found that when ut is 86200, the corresponding...
3
by: G | last post by:
Hi ,you guys: look at the code below //////////////////////////////////////////////////////////////////////////////////////////////// #include<iostream> using namespace std; int main() {
2
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.