![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How to exchange the Lower and Upper Nibbles of a floating point numberHi 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
|
|||
|
|||
Re: How to exchange the Lower and Upper Nibbles of a floating point numberQuote:
CPP / C++ / C Code:
Output (from my little-endian PC): Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: How to exchange the Lower and Upper Nibbles of a floating point numberQuote:
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 GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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