Writing float values to hex
Writing float values to hex
Ok, I've seen the binary representation of a floating point number, and let me just say it's not pretty. Is there a simpler way to convert a floating point number into its hexadecimal equivalent without having to find out mantissas and 2s compliments and stuff? It would really help if I could get this in VB code.
-
- Posts: 325
- Joined: Mon Mar 22, 2004 3:59 pm
- Location: Everywhere but nowhere
- Contact:
-
- Posts: 118
- Joined: Wed Mar 30, 2005 5:35 pm
If your using .Net:
If your using C\C++ you can just type cast it to a long and use itoa to get the hex value in a string
If you using VB6. I recommend you upgrade to VB.NET or even better, C#.NET
Code: Select all
public static void GetBytesSingle( float argument )
{
const string formatter = "{0,16:E7}{1,20}";
byte[] byteArray = BitConverter.GetBytes( argument );
Console.WriteLine( formatter, argument, BitConverter.ToString(byteArray) );
}
GetBytesSingle( 1.0F ); // outputs "00-00-80-3F"
GetBytesSingle( 15.0F ); // outputs "00-00-70-41"
If you using VB6. I recommend you upgrade to VB.NET or even better, C#.NET