473,670 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3213

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_byte s = &given_double_v ariable ;
int* last_four_bytes = first_four_byte s + 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_give n_integer ;

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

if ( one_bit_in_give n_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_binar y.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_give n_integer ;

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

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

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

double some_double_num ber = 1234.567 ;

int* first_four_byte s = (int*) &some_double_nu mber ;
int* last_four_bytes = first_four_byte s + 1 ;

print_in_binary _form( *first_four_byt es ) ;
print_in_binary _form( *last_four_byte s ) ;
}
Jul 22 '05 #4
> double some_double_num ber = 1234.567 ;

int* first_four_byte s = (int*) &some_double_nu mber ;
int* last_four_bytes = first_four_byte s + 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_cas t 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"
<fraserATmember s.v21.co.united kingdom> wrote in comp.lang.c++:
double some_double_num ber = 1234.567 ;

int* first_four_byte s = (int*) &some_double_nu mber ;
int* last_four_bytes = first_four_byte s + 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.l earn.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_binar y.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_give n_byte ;

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

if ( one_bit_in_give n_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_give n_integer ;

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

if ( one_bit_in_give n_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_nu mber = (char*) &test_number ;

for ( int byte_counter = 0 ;
byte_counter < sizeof( double ) ;
byte_counter ++ )
{
cout << " " ;
print_in_binary _form( *byte_in_test_n umber ) ;
byte_in_test_nu mber ++ ;
}

cout << "\n\n" ;

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

int* first_four_byte s = (int*) &test_number ;
int* last_four_bytes = first_four_byte s + 1 ;

print_in_binary _form( *first_four_byt es ) ;

cout << " " ;

print_in_binary _form( *last_four_byte s ) ;

}
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
9740
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) + (ord(c)<<16) + (ord(c)<<24)
1
4384
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 exactly: numeric(10, 5) and numeric. Given that the structure to fill-up is DBNUMERIC defined as:
2
3625
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; http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe I copied this code and modified it slightly, however, you will notice that for one of the examples, the conversion isn't...
14
15271
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 will be rounded off. For doubles, the mantissa is 53 bits = 2^53-1 max value = 9,007,199,254,740,991.0l (that's an L). So 16 digit odd numbers greater than that will be rounded off. To get the actual precision we take log(base 10) of those...
6
7607
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, Karthi
6
2911
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 (100,000+ float values) I don't intend to store all of the 100,000 datapoints, but rather a subset of say 250. Due to the volume of testing that we need and the volume of results stored, I need to be VERY careful with data size and keep things...
116
35840
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
9
3914
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 buffer can con contain other things besides this float int* val=reinterpret_cast<int*>(&deneme); // gives me jeebies vec.push_back((unsigned char) ((*val >24) & 0xFF ));
19
4770
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, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
0
8466
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7412
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.