Member Avatar for BlackJax96

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?

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();
        }
    }
}
commented: Nice! +8
Member Avatar for BlackJax96

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

Please,
Have you succeeded?

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.