Solved

Float conversion to 4 bytes?

Posted on 1998-11-17
5
261 Views
Last Modified: 2013-11-20
I need to comvert 1 float into 4 bytes and then back to 1 float again.  Someone told me to try this:
FLOAT f1 = 123.456, f2 = 0.0 ;
BYTE byByte1 = HIBYTE( HIWORD( f1 ) ) ;
BYTE byByte2 = LOBYTE( HIWORD( f1 ) ) ;
BYTE byByte3 = HIBYTE( LOWORD( f1 ) ) ;
BYTE byByte4 = LOBYTE( LOWORD( f1 ) ) ;
//rebuild
f2  = byByte1 << 24 | byByte2 << 16 | byByte3 << 8 | byByte4 ;

The rebuild answer for this is f2 = 123.000 and not 123.456.  How can a get the rest of my float?  What am I doing wrong? Also what is required to break down to bytes, a double (8bytes) and rebuild the 8 bytes into one double? All sugestions welcome and sample code is greatly appreciated!  
0
Comment
Question by:Surfer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:Surfer
ID: 1324810
Edited text of question
0
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1324811
I don't understand what you want to do.
variable (as you probably know) of type float takes 4 bytes in memory. So if you need access to each byte of them:

BYTE *p = (BYTE*)&f1;
p[0] - first byte and so on.

the code in your question doesn't work because bit operation OR can't return float type.

may be something like this
      FLOAT f1 = 123.456, *f2;
      int *pf, i2;
      f2 = new FLOAT;
      pf = (int*)&f1;
      BYTE byByte1 = HIBYTE( HIWORD( *pf ) ) ;
      BYTE byByte2 = LOBYTE( HIWORD( *pf ) ) ;
      BYTE byByte3 = HIBYTE( LOWORD( *pf ) ) ;
      BYTE byByte4 = LOBYTE( LOWORD( *pf ) ) ;
      //rebuild
      i2  = byByte1 << 24 | byByte2 << 16 | byByte3 << 8 | byByte4;
      memcpy((void*)f2, (void*)&i2, 4);

0
 

Author Comment

by:Surfer
ID: 1324812
I tried to modify your code sample that works with type float to work with type double and it dose not work.  All suggestions welcome.

double r1 = 123.456, *r2 ;
r2 = new double ;
_int64 i2 ;

BYTE *pr = (BYTE*)&r1 ;

BYTE byByte1 = pr[7] ;
BYTE byByte2 = pr[6] ;
BYTE byByte3 = pr[5] ;
BYTE byByte4 = pr[4] ;
BYTE byByte5 = pr[3] ;
BYTE byByte6 = pr[2] ;
BYTE byByte7 = pr[1] ;
BYTE byByte8 = pr[0] ;

i2 = byByte1 << 56 | byByte2 << 48 | byByte3 << 40 | byByte4  << 32 | byByte5 << 24 | byByte6 << 16 | byByte7 << 8  | byByte8 ;

memcpy((void*)r2,(void*)&i2,8) ;            

delete r2 ;
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1324813
Your problem is due to the way floats are stored internally - IEEE format.
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1324814
#include <windows.h>
#include <stdio.h>

float f1 = 123.456;
float f2;
double d1 = 345.6789;
double d2;
BYTE *b;
BYTE *d;

void main()
{

  b = (BYTE *)&f1;
  printf("Float Bytes:   %i %i %i %i\n",b[0],b[1],b[2],b[3]);
  f2 = *(float *)b;
  printf("First float:  %f  \nSecond float:  %f\n\n",f1,f2);
  d = (BYTE *)&d1;
  printf("Double bytes:  %i %i %i %i %i %i %i %i\n",d[0],d[1],d[2],d[3],d[4],d[5],
       d[6],d[7]);
  d2 = *(double *)d;
  printf("First double:  %f  \nSecond double:  %f   ",d1,d2);

}

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 121
iSeries FTP Exit Program 8 163
centeredAverage challenge 8 185
JQuery serialize and unserialize 8 188
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question