Convert double into hex in C#

20,277

Solution 1

Well, I googled for a minute or two and according to this here is a quite ellegant solution

    double d = 12.09;
    Console.WriteLine("Double value: " + d.ToString());
    byte[] bytes = BitConverter.GetBytes(d);
    Console.WriteLine("Byte array value:");
    Console.WriteLine(BitConverter.ToString(bytes));

Solution 2

You can convert base 10 to base 16 by continually multiplying the fraction by 16, stripping out the 'whole' number, and repeating with the remainder.

So to convert 0.1 Decimal to Hex

0.1 * 16
= 1.6

So 1 becomes the first hex value. Keep going with the remaining 0.6

0.6 * 16 = 9.6

So 9 becomes the second hex value. Keeping going with the remaining 0.6

0.6 * 16 = 9.6

etc.

So 0.1 Decimal = 0.19999.. recurring hex

From memory this works for any radix. Obviously in hex a whole value of 10 would be A etc.

Solution 3

BitConverter.DoubleToInt64Bits(value).ToString("X")

Solution 4

Assuming you want to convert to hexadecimal base/radix, the following should do the trick:

static void Main(string[] args)
{
    Console.WriteLine(Base16(135.34375, 10));
    Console.ReadLine();
}

private static string Base16(double number, int fractionalDigits)
{
    return Base(number, fractionalDigits, new char[]{
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F' });
}

private static string Base(double number, int fractionalDigits, params char[] characters)
{
    int radix = characters.Length;
    StringBuilder sb = new StringBuilder();

    // The 'whole' part of the number.
    long whole = (long)Math.Floor(number);
    while (whole > 1)
    {
        sb.Insert(0, characters[whole % radix]);
        whole = whole / radix;
    }

    // The fractional part of the number.
    double remainder = number % 1;
    if (remainder > Double.Epsilon || remainder < -Double.Epsilon)
    {
        sb.Append('.');

        double nv;
        for (int i = 0; i < fractionalDigits; i++)
        {
            nv = remainder * radix;
            if (remainder < Double.Epsilon && remainder > -Double.Epsilon)
                break;
            sb.Append(characters[(int)Math.Floor(nv)]);
            remainder = nv % 1;
        }
    }

    return sb.ToString();
}

The hexadecimal conversion of 135.34375 is 87.58.

Share:
20,277
rross
Author by

rross

Updated on August 06, 2022

Comments

  • rross
    rross over 1 year

    I have this value:

    double headingAngle = 135.34375;
    

    I would like to convert it to HEX and print the HEX to the console. I have already converted a string and int into their respective HEX values, but a double seems to be much more tricky. Can someone point me in the right direction?

  • Simon Robinson
    Simon Robinson almost 11 years
    I know this question is quite old, but it's probably worth pointing out that this code won't convert the value of the double to its hexadecimal representation. Rather, it'll write out the hex representation of the bits that are used to store the number internally. For a floating point number, the two are different things!
  • Bart
    Bart almost 11 years
    Point taken. Although I didn't heard of any standard of hex notation for representation of floating point values. Of course it is important to remember about the hardware spec and all the big/low endian stuff. However standard IEEE 754 is very popular and widely used. So..