2017-08-16 33 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

相关问题