0

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?

4
Contributors
4
Replies
9
Views
6 Years
Discussion Span
Last Post by Ketsuekiame
Featured Replies
  • 2

    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: [code]using System; using System.Text; namespace Text { class Program { static void Main() { float f = -2.07217f; byte[] b = BitConverter.GetBytes(f); … Read More

2

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();
        }
    }
}
Votes + Comments
Nice!
0

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.");
                        }
                    }
                }

Edited by BlackJax96: n/a

0

Please,
Have you succeeded?

I have the same problem, my code works for the 64-bit but not for the 32 bits.

0

Please start your own thread. This question is marked solved and is over two years old.

A new thread is more likely to be read and an answer to your question given.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.