473,406 Members | 2,217 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,406 software developers and data experts.

Writing a float or double's byte values

Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil
Jul 22 '05 #1
8 3202

Philipp wrote:

Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil


The program that is copied below prints an int variable
in binary form. It could be modified to work with double
variables. but probably you need to use pointers to
access the bytes of a double. The following MAY work

int* first_four_bytes = &given_double_variable ;
int* last_four_bytes = first_four_bytes + 1 ;

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.oamk.fi/~karil/
http://www.naturalprogramming.com/
// binary.cpp (c) 1998-2002 Kari Laitinen

// This program is from a book. More information at
// http://www.naturalprogramming.com/cppbook.html

#include <iostream.h>

void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}
int main()
{
unsigned int test_number = 0x9A9A ;

cout << "\n Original test number: " ;
print_in_binary_form( test_number ) ;

cout << "\n Twice left-shifted form: " ;
test_number = test_number << 2 ;
print_in_binary_form( test_number ) ;

cout << "\n Back to original form: " ;
test_number = test_number >> 2 ;
print_in_binary_form( test_number ) ;

cout << "\n Last four bits zeroed: " ;
test_number = test_number & 0xFFF0 ;
print_in_binary_form( test_number ) ;

cout << "\n Last four bits to one: " ;
test_number = test_number | 0x000F ;
print_in_binary_form( test_number ) ;

cout << "\n A complemented form: " ;
test_number = ~test_number ;
print_in_binary_form( test_number ) ;

cout << "\n Exclusive OR with 0xF0F0:" ;
test_number = test_number ^ 0xF0F0 ;
print_in_binary_form( test_number ) ;
}
Jul 22 '05 #2

"Philipp" <no*************@hotmail.com> wrote in message
news:41********@epflnews.epfl.ch...
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?


union
{
unsigned char b[8];
double d;
} u;

u.d= 1.2345;
cout << hex << setfill('0');
for (int i = 0; i < 8; ++i)
cout << u.b[i] << ' ';

Untested code.

john
Jul 22 '05 #3

Philipp wrote:

Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil

Below is a version of my program which seems to
do the job. I compiled it with Borland C++ 5.5.1.

The advice in my previous message were a little bit
inaccurate.

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.oamk.fi/~karil/ + 358 40 566 0869
http://www.naturalprogramming.com/

// double_to_binary.cpp (c) 1998-2002 Kari Laitinen

// This program is from a book. More information at
// http://www.naturalprogramming.com/cppbook.html

#include <iostream.h>

void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}
int main()
{

double some_double_number = 1234.567 ;

int* first_four_bytes = (int*) &some_double_number ;
int* last_four_bytes = first_four_bytes + 1 ;

print_in_binary_form( *first_four_bytes ) ;
print_in_binary_form( *last_four_bytes ) ;
}
Jul 22 '05 #4
> double some_double_number = 1234.567 ;

int* first_four_bytes = (int*) &some_double_number ;
int* last_four_bytes = first_four_bytes + 1 ;


An implementation does not have to use 32 bits with ints. I think they do
have to use 64 bits for double. I would reinterpret_cast a pointer to
double to a pointer to unsigned __int64 or whatever keyword a compiler uses.
__int64 is a Borland keyword. It isn't portable but it is easily changed
and quaranteed to produce an error if a compiler hasn't got the keyword.

Fraser.
Jul 22 '05 #5
Kari Laitinen wrote:
Philipp wrote:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil


Below is a version of my program which seems to
do the job. I compiled it with Borland C++ 5.5.1.

The advice in my previous message were a little bit
inaccurate.


Both answers provide what I was looking for and work great. Thank you
very much!
Best regards Phil
Jul 22 '05 #6
On Fri, 8 Oct 2004 14:47:01 +0100, "Fraser Ross"
<fraserATmembers.v21.co.unitedkingdom> wrote in comp.lang.c++:
double some_double_number = 1234.567 ;

int* first_four_bytes = (int*) &some_double_number ;
int* last_four_bytes = first_four_bytes + 1 ;


An implementation does not have to use 32 bits with ints. I think they do
have to use 64 bits for double.


You think wrong. Completely wrong.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7

Philipp wrote:

Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00

Is there a function which does this or anything approaching?

I read that on win32 the representation of 8byte double is
byte1 byte2 byte3 byte4 byte8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM

with S= signe bit
X = exponant bits
M = mantissa bits

That's the values I'd like to get.
Thanks for your help Phil

This is my third solution to your
problem. Below is a copy of a program that
should print the bytes of a double variable
in correct order. My previous solution was
such that it works only in a few computers.

I hope this works. There are, however, no errors in
the programs of the mentioned book.

--
Mr. (Dr.) Kari Laitinen
Oulu Institute of Technology, Finland
http://www.naturalprogramming.com/
// double_to_binary.cpp (c) 1998-2004 Kari Laitinen

// This program is from a book. More information at
// http://www.naturalprogramming.com/cppbook.html

#include <iostream.h>

void print_in_binary_form( char given_byte )
{
unsigned char bit_mask = 0x80 ;
unsigned char one_bit_in_given_byte ;

for ( int bit_counter = 0 ;
bit_counter < 8 ;
bit_counter ++ )
{
one_bit_in_given_byte = given_byte & bit_mask ;

if ( one_bit_in_given_byte == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}
void print_in_binary_form( int given_integer )
{
// This program works only with 32-bit int variables.
// To make this program work with 16-bit int variables,
// you should use initial mask 0x8000 and let the loop
// be executed only 16 times.

unsigned int bit_mask = 0x80000000 ;
unsigned int one_bit_in_given_integer ;

for ( int bit_counter = 0 ;
bit_counter < 32 ;
bit_counter ++ )
{
one_bit_in_given_integer = given_integer & bit_mask ;

if ( one_bit_in_given_integer == 0 )
{
cout << "0" ;
}
else
{
cout << "1" ;
}

bit_mask = bit_mask >> 1 ;
}
}
int main()
{
cout << "\n\n" ;

double test_number = 123.456 ;

char* byte_in_test_number = (char*) &test_number ;

for ( int byte_counter = 0 ;
byte_counter < sizeof( double ) ;
byte_counter ++ )
{
cout << " " ;
print_in_binary_form( *byte_in_test_number ) ;
byte_in_test_number ++ ;
}

cout << "\n\n" ;

cout << "\n The following is not correct: \n" ;

int* first_four_bytes = (int*) &test_number ;
int* last_four_bytes = first_four_bytes + 1 ;

print_in_binary_form( *first_four_bytes ) ;

cout << " " ;

print_in_binary_form( *last_four_bytes ) ;

}
Jul 22 '05 #8
"John Harrison" <jo*************@hotmail.com> wrote:
"Philipp" <no*************@hotmail.com> wrote:
Hello,
I would like to print (cout) the binary representation of a double.
ie the values of the bytes 1-8.
A typical output would be something like (in hex)
ff af 12 d3 ab 9f 3c 00


union
{
unsigned char b[8];
double d;
} u;

u.d= 1.2345;
cout << hex << setfill('0');
for (int i = 0; i < 8; ++i)
cout << u.b[i] << ' ';


Undefined behaviour (you access u.b but the last member set was u.d)
Also you should replace '8' with 'sizeof(double)'.

Better would be:

double d = 1.2345;
for (int i = 0; i != sizeof(double); ++i)
cout << ((unsigned char *)&d)[i] << ' ';

or some optimised variation of that.
Jul 22 '05 #9

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

Similar topics

9
by: franzkowiak | last post by:
Hello, I've read some bytes from a file and just now I can't interpret 4 bytes in this dates like a real value. An extract from my program def l32(c): return ord(c) + (ord(c)<<8) +...
1
by: Giovanni Azua | last post by:
Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in C. As result my C dll program produces two float values, the TSQL side expects to have...
2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
14
by: ziller | last post by:
Why is it that FLT_DIG (from <float.h>) is 6 while DBL_DIB is 15? Doing the math, the mantissa for floats is 24 bits = 2^24-1 max value = 16,777,215.0f. Anything 8-digit odd # greater than that...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
6
by: Steve | last post by:
Hi, I've developed a testing application that stores formatted results in a database. Recently it was requested that I add the ability to generate graphs from the raw, un formatted test results...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
9
by: hurcan solter | last post by:
Hi all, I am trying to convert a float value to a octet stream for transmission, I came up with solution like float deneme=3.14156789; float deneme2=0.0; vector<unsigned charvec; //this is my...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.