我有一個4 unsigned int
條目具有相同的值的數組255
我想將它們轉換爲decimal
值。下面是代碼:小數構造函數拋出System.ArgumentException當我傳遞int數組4個元素
public static decimal BytesToDecimal(byte[] buffer, int offset = 0)
{
var decimalBits = new int[4];
decimalBits[0] = buffer[offset + 0] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16) | (buffer[offset + 3] << 24);
decimalBits[1] = buffer[offset + 4] | (buffer[offset + 5] << 8) | (buffer[offset + 6] << 16) | (buffer[offset + 7] << 24);
decimalBits[2] = buffer[offset + 8] | (buffer[offset + 9] << 8) | (buffer[offset + 10] << 16) | (buffer[offset + 11] << 24);
decimalBits[3] = buffer[offset + 12] | (buffer[offset + 13] << 8) | (buffer[offset + 14] << 16) | (buffer[offset + 15] << 24);
return new decimal(decimalBits);
}
static void Main(string[] args)
{
decimal dd = BytesToDecimal(new byte[16] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 });
Console.WriteLine("{0}", dd.ToString());
}
我期待的結果-1
;但是,該程序引發以下異常。
未處理的異常信息:System.ArgumentException:十進制字節數組 構造需要含有VAL ID小數 字節長度爲4的陣列。
本部分是否包含任何跨平臺的代碼?據我所知,'System.Decimal'的內部表示是不可移植的。 – Dai