473,387 Members | 3,750 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,387 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 11976
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.