Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# converting from float to hexstring via IEEE 754 and back to float

Tags:

c#

I need to convert a float value to a hexadecimal string and back again. The online IEEE 754 Converter allows me to do that. Here is my attempt to implement the conversion:

unsafe static void Main(string[] args)
{
    float f = 0.0023984962F;
    long l = *((long*)&f);
    float r = *((float*)&l);

    Console.WriteLine("{0} <-> {1:X} <-> {1:F1} <-> {2:F1}", f, l, r);
    Console.ReadLine();
}

My questions:

  1. With the float 0.0023984962 the expected result is 0x3B1D3017 but my code prints 0x22CB20C3B1D3017. What am I doing wrong?

  2. How can I save the output in a variable?

like image 557
user1351841 Avatar asked Jan 26 '26 18:01

user1351841


2 Answers

To be able to convert both ways you need two functions. You have a bug in your code where you cast a 32 bit floating point number (float) to a 64 bit integer (long). Because your code is unsafe this bug is not caught by the compiler.

However, using unsafe code performs very well so here is code that works:

unsafe string ToHexString(float f) {
  var i = *((int*) &f);
  return "0x" + i.ToString("X8");
}

unsafe float FromHexString(string s) {
  var i = Convert.ToInt32(s, 16);
  return *((float*) &i);
}

To avoid the problems of unsafe code you can also use a BitConverter which will check the size of the intermediate byte buffers for you.

string ToHexString(float f) {
  var bytes = BitConverter.GetBytes(f);
  var i = BitConverter.ToInt32(bytes, 0);
  return "0x" + i.ToString("X8");
}

float FromHexString(string s) {
  var i = Convert.ToInt32(s, 16);
  var bytes = BitConverter.GetBytes(i);
  return BitConverter.ToSingle(bytes, 0);
}

You can now convert both ways:

var hexString = ToHexString(0.0023984962F);
var f = FromHexString(hexString);
like image 101
Martin Liversage Avatar answered Jan 28 '26 06:01

Martin Liversage


You should use int, not long when converting float, since float (aka Single) is 32-bit value (unlike double which is 64-bit):

unsafe static void Main(string[] args)
{
    float f = 0.0023984962F;
    int l = *((int*)&f); // int here: 32-bit float into 32-bit integer
    float r = *((float*)&l);

    // Let's save the result in the variable
    String result = String.Format("{0} <-> {1:X} <-> {1:F1} <-> {2:F1}", f, l, r);

    Console.WriteLine(result);
    Console.ReadLine();
}

However, a better approach is to use BitConverter that's specially designed to that role and avoid unsafe routine:

static void Main(string[] args) {
  float f = 0.0023984962F;

  uint l = BitConverter.ToUInt32(BitConverter.GetBytes(f), 0);
  float r = BitConverter.ToSingle(BitConverter.GetBytes(l), 0);

  // Let's save the result in the variable
  String result = String.Format("{0} <-> {1:X} <-> {1:F1} <-> {2:F1}", f, l, r);

  Console.WriteLine(result);
  Console.ReadLine();
}
like image 38
Dmitry Bychenko Avatar answered Jan 28 '26 06:01

Dmitry Bychenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!