2017-08-16 127 views
0

如何從double類型提取字節。我知道它有8個字節,就像long。我怎樣才能創建一個longdouble具有相同字節的變量。C#如何從雙提取字節

double a = 1.5; 
long b = (long)a; // <- this returns 1 
// i want to get this: 0 01111111111 1000000000000000000000000000000000000000000000000000 
//which is 4609434218613702656 as long (I guess :)) 

我該如何快速做到這一點?

+2

的可能的複製[如何獲得一個「雙」作爲一個「長」](https://stackoverflow.com/questions/4475611/how-to-get-the-bits雙倍長的) – harold

回答

2

你可以得到它這樣

double a = 1.5; 
long l = BitConverter.ToInt64(BitConverter.GetBytes(a), 0); 

這將是4609434218613702656

由於@harold建議

var l2 = BitConverter.DoubleToInt64Bits(a); 

也是可能的

+0

BitConverter也有DoubleToInt64Bits – harold