Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
How to convert a double number to hex againg
 
hex to long .
Posted 2-Mar-10 20:30pm
Edited 2-Mar-10 22:12pm
CPallini310.3K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hex is a system of representation, not a number. You can use a format specifier on a number to turn it into a hex string, via the ToString method. I believe it is x. You may be able to use a format specifier in long.TryParse or long.Parse to get it back to a number.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think you should first convert the double to a byte array, see the example in the "BitConverter.GetBytes Method" documentation.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

For example when i convert 0.32456 to hex it is giving error as invalid format specified..
pls help
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

internal static string DoubleToHex(double value, int maxDecimals)
{
    string result = string.Empty;
    if (value < 0)
    {
        result += "-";
        value = -value;
    }
    if (value > ulong.MaxValue)
    {
        result += double.PositiveInfinity.ToString();
        return result;
    }
    ulong trunc = (ulong)value;
    result += trunc.ToString("X");
    value -= trunc;
    if (value == 0)
    {
        return result;
    }
    result += ".";
    byte hexdigit;
    while ((value != 0) && (maxDecimals != 0))
    {
        value *= 16;
        hexdigit = (byte)value;
        result += hexdigit.ToString("X");
        value -= hexdigit;
        maxDecimals--;
    }
    return result;
}
 
internal static ulong HexToUInt64(string hex)
{
    ulong result;
    if (ulong.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result))
    {
        return result;
    }
    throw new ArgumentException("Cannot parse hex string.", "hex");
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 Richard Deeming 215
3 CHill60 170
4 _Amy 145
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,067
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Mar 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100