I have following code.
float b;
b= 1.234;
cout<<hex<<b<<endl;
How come it cannot ouput the hex representation of b? 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
"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
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++.
"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
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
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
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;
}
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
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
"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
"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
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
"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
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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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);
|
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...
|
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...
|
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...
|
by: G |
last post by:
Hi ,you guys:
look at the code below
////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
using namespace std;
int main()
{
|
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>
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |