954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting Float to 32bit Hex

Hi, I want to convert a float value to 32bit hex and the code I have right now returns 64bit hex. The float is stored in a string called value.

result = Convert.ToDouble(value);
long lVal = BitConverter.DoubleToInt64Bits(result);
string hex = lVal.ToString("X");
OutputBox.Text += ("\nConverted the " + value.GetType().Name + " value '" + value + "' to the " + hex.GetType().Name + " value " + hex + ".");

Example output: Converted the String value '-2.07217' to the String value C00093CDDD6E04C0.

How would I do this?

BlackJax96
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I'm not sure what you want. Is it the hex representation of the bits used to represent the float value? If so, you can use something like this:

using System;
using System.Text;

namespace Text {
    class Program {
        static void Main() {
            float f = -2.07217f;

            byte[] b = BitConverter.GetBytes(f);
            StringBuilder sb = new StringBuilder();
            foreach (byte by in b) {
                sb.Append(by.ToString("X"));
            }


            Console.WriteLine(sb.ToString());

            Console.ReadLine();
        }
    }
}
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

That appears to work, but I know that
7.18786 = 40E602F3

And this is giving me
7.18786 = F32E640

How would I perform the conversion above? Here is my code for 64bit conversion:

using (XmlReader reader = XmlReader.Create(daestream))
                {
                    reader.MoveToContent();
                    reader.ReadToFollowing("float_array");
                    string floats = reader.ReadInnerXml();
                    string[] values = floats.Split(" ".ToCharArray());
                    double result;
                    foreach (string value in values)
                    {
                        try
                        {
                            result = Convert.ToDouble(value);
                            long lVal = BitConverter.DoubleToInt64Bits(result);
                            string hex = lVal.ToString("X");
                            OutputBox.Text += ("\nConverted the " + value.GetType().Name + " value '" + value + "' to the " + hex.GetType().Name + " value " + hex + ".");
                        }
                        catch (FormatException)
                        {
                            OutputBox.Text += ("\nThe format is incorrect.");
                        }
                        catch (OverflowException)
                        {
                            OutputBox.Text += ("\nThe float values are too large.");
                        }
                    }
                }
BlackJax96
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: