GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-May-2008, 22:57
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 22
vicky_brsh has a little shameless behaviour in the past

How to exchange the Lower and Upper Nibbles of a floating point number


Hi all,

We have got some data (binary Data) generated in a file which is generated from SunSolaris which is a Big Endian Machine.

But we have to process the data in Linux which is a Little Endian machine.

I can understand that if we have integer variables we can read the file and scan the whole file using
fscanf("%d"...)
....
and exchange the lower nibble and upper nibbles by bitwise operators.

But the problem is the data contains only floating point values.

i have 2 questions here
1) How is the floating point numbers stored as binaries assuming sizeof float is 4 Bytes can i read it as a character and combine 4 characters to yiels a floating number ?
2) if i do step 1 how can i exchange the nibbles and also how to convert the 4 characters read to a floating number ?

Can any one help me to solve this problem ?

Thanks in Advance
Vikram
  #2  
Old 14-May-2008, 23:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to exchange the Lower and Upper Nibbles of a floating point number


Quote:
Originally Posted by vicky_brsh
We have got some data (binary Data) generated in a file which is generated from SunSolaris which is a Big Endian Machine
Assuming that the floating point representation is identical (bit for bit) except for byte endianness, here is one way:
CPP / C++ / C Code:
#include <stdio.h>

typedef union {
    float f;
    unsigned char  c[sizeof(float)];
}float_char;

int main()
{
    unsigned i;
    unsigned j;
    float temp;

    float_char fc;

    float x = 12.34;
    fc.f = x;

    printf("Here are the bytes of %f, as stored in memory:\n   ", x);
    for (i = 0; i < sizeof(float); i++) {
        printf("0x%02x ", fc.c[i]);
    }
    printf("\n\n");

    /* reverse the order of the chars */
    for (i = 0, j = sizeof(float)-1; i < j; i++, j--) {
        temp = fc.c[i];
        fc.c[i] = fc.c[j];
        fc.c[j] = temp;
    }
    printf("After reversing the bytes:\n   ");
    for (i = 0; i < sizeof(float); i++) {
        printf("0x%02x ", fc.c[i]);
    }
    printf("\n");
    return 0;
}

Output (from my little-endian PC):
Code:
Here are the bytes of 12.340000, as stored in memory: 0xa4 0x70 0x45 0x41 After reversing the bytes: 0x41 0x45 0x70 0xa4

Regards,

Dave
  #3  
Old 15-May-2008, 09:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,620
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to exchange the Lower and Upper Nibbles of a floating point number


Quote:
Originally Posted by davekw7x
.
CPP / C++ / C Code:
typedef union {
    float f;
    unsigned char  c[sizeof(float)];
}float_char;

int main()
{
    float temp; /* <--- Why the heck didn't I declare it an unsigned char???? */

    float_char fc;
.
.
        temp = fc.c[i];
        fc.c[i] = fc.c[j];
        fc.c[j] = temp;
.

Well, that's just plain silly. (Not actually "wrong," since it works, but really, really silly.) See Footnote.

I really should have declared temp to be an unsigned char, since I am using it to swap values in an array of unsigned chars.

I'm sorry if this caused any confusion or if it offended anyone's sense of elegance in any way. (I really hate it when I found that I have done something like this.)

Regards,

Dave

Footnote:
Why did I say that it "works" as I originally posted? Because all possible values within the range of an unsigned char are integers that can be represented exactly as floating point values. So nothing is lost in converting from an unsigned char to and from a float. But, even though it "works," it's just not the "thing to do," and I regret having posted it with that "cosmetic" flaw.

Sometimes, when I test a program and it "works," I just don't pay enough attention to details like this. This is actually a cut-down copy/paste from an collection of application functions that I have been using for years, and I didn't notice that, when I renamed some variables for purpose of this response, I wasn't consistent with my intent.
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Numbers Generation alcoholic CPP / C++ Forum 10 29-Jan-2006 11:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.