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");
}