C#で8バイト長の16進数を浮動小数点数に変換する必要があります。たとえば :C#で16進数をIEEE 754浮動小数点に変換
4030000000000000は16.0
C0622EB860000000は-14.46
4090000000000000する必要がありますする必要があります1024.0
である必要があり、私が動作しているようにこのコードを発見したが、それは置いていません10より大きく-10より小さい数字の場合は、 の点が正しく表示されます。たとえば、 -14.46は-1.4546と表示されます。コードの何が間違っていますか?
const string formatter = "{0,20}{1,27:E16}";
// Reinterpret the long argument as a double.
public static void LongBitsToDouble(long argument)
{
double doubleValue;
doubleValue = BitConverter.Int64BitsToDouble(argument);
// Display the argument in hexadecimal.
Console.WriteLine(formatter, String.Format("0x{0:X16}", argument),
doubleValue);
}
public static void Main()
{
Console.WriteLine("This example of the BitConverter.Int64BitsToDouble("
+"long) \nmethod generates the following output.\n");
Console.WriteLine(formatter, "long argument","double value");
Console.WriteLine("-------------");
// Convert long values and display the results.
LongBitsToDouble(unchecked((long)0x4030000000000000)); //16.0
LongBitsToDouble(unchecked((long)0xC0622EB860000000)); //-14.46
Console.ReadKey();
}
コードに問題はありません。それは指数ie:E + 008も表示します。例えば、10.0は1.0E + 001として印刷することができます。 – Deolus
指数部が不要な場合は、それを 'formatter'から削除します。 IE: '{0,20} {1,27} 'に変更してください; – Deolus
Deolusに感謝!それは動作します!私はあなたの答えに投票しただろうが、私はどのように見ることができません。 – Marian