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

Hex to IEEE 754

Hello,

I am trying to convert a hex value "4B3C614E" to a IEEE 754 floating point value. The correct answer to 0x4B3C614E is 12345678.00. However in C#,
I keep getting the value 1262248270. I do not have any idea how to get the
12345678.00 like is should be per the site: http://babbage.cs.qc.edu/IEEE-754/Decimal.html . My code is below:

int myHex = 0x4b3c614e;
float myFloat = System.Convert.ToSingle(myHex);

-qgeorge
Sep 27 '06 #1
5 11975
Banfa
9,065 Expert Mod 8TB
This C code seems to work for me

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     float f = 12345678.0F;
  6.     unsigned long l = *((unsigned long *)&f);
  7.     float r = *((float *)&l);
  8.  
  9.     printf("%08lx <-> %.1f\n", l, r );
  10.  
  11.     return 0;
  12. }
  13.  
Note, this is completely non-portable because C does not even guarantee that a float * has the same number of bits as a unsigned long * and if they do it doesn't guarantee that they use the same bit representation.
Sep 27 '06 #2
Thanks, But I do not have a problem getting the correct answer in C. I have a poblem getting the correct answer in C#.
Sep 27 '06 #3
Banfa
9,065 Expert Mod 8TB
And here is a mildly interesting C# attempt

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         unsafe static void Main(string[] args)
  10.         {
  11.             float f = 12345678.0F;
  12.             long l = *((long *)&f);
  13.             float r = *((float *)&l);
  14.  
  15.             Console.WriteLine("{0:F1} <-> {1:X} <-> {1:F1} <-> {2:F1}", f, l, r);
  16.         }
  17.     }
  18. }
  19.  
Output

12345680.0 <-> 4B3C614E <-> 1262248270.0 <-> 12345680.0

So itr sort of gets it right except when I tell it to treat a long variable as though it were a float.
Sep 27 '06 #4
Thanks!!!!!!
Sep 27 '06 #5
If using c#, why not use the BitConverter class.

In VB, it would look like this:

Dim bytes() As Byte = {&H4E, &H61, &H3C, &H4B}

Dim result As Single = BitConverter.ToSingle(bytes, 0)



more can be found at:

http://msdn2.microsoft.com/en-us/library/system.bitconverter.aspx
Dec 6 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Mad Butch | last post by:
void Test() { float fValue = 0.5678f; // Value is 0.567800 double dValue = (double)fValue; // Value is 0.56779998540878 } Is there anyway I can round a float at 6 positions behind the...
11
by: Grant Edwards | last post by:
I've read over and over that Python leaves floating point issues up to the underlying platform. This seems to be largely true, but not always. My underlying platform (IA32 Linux) correctly...
2
by: Ingo Nolden | last post by:
Hi, I am not really sure wether there is a better group for this. I already tried a VC group, but they cannot tell me how other stdlibs are doing it. I tested a lot with inf and NaNs. I read...
1
by: GooglePoster | last post by:
Hello, I am looking for a function/utility to read in an IEEE value and convert this to decimal, for testing purposes. Also, I need to convert decimal values to IEEE values to send out on a...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
2
by: Andre | last post by:
I read some place that for performance reasons, IEEE floating point standard has not been implemented by .NET and that different machine architectures may produce different results.. but what if...
33
by: Nick Maclaren | last post by:
The numerical robustness of Python is very poor - this is not its fault, but that of IEEE 754 and (even more) C99. In particular, erroneous numerical operations often create apparently valid...
13
by: revuesbio | last post by:
Hi Does anyone have the python version of the conversion from msbin to ieee? Thank u
5
by: ms292606 | last post by:
Hi, I am currently working on a program to decode data that is collected on a gps receiver and i've ran into a problem. Some of the data is encoded in IEEE 754. I wrote the following functions to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.