12,510,403 members (51,622 online)
Rate this:
See more:
How to convert a double number to hex againg

hex to long .
Posted 2-Mar-10 20:30pm
Updated 2-Mar-10 22:12pm
CPallini389.7K
v2

Rate this:

## 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.
v2
Rate this:

## Solution 3

I think you should first convert the double to a byte array, see the example in the "BitConverter.GetBytes Method" documentation.
Rate this:

## 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");
}```
Rate this:

## Solution 2

For example when i convert 0.32456 to hex it is giving error as invalid format specified..
pls help

Top Experts
Last 24hrsThis month
 Maciej Los 215 OriginalGriff 191 John Simmons / outlaw programmer 175 CPallini 140 David_Wimbley 135
 OriginalGriff 7,927 Maciej Los 4,091 ppolymorphe 4,048 David_Wimbley 3,331 Karthik Bangalore 3,161