2017-08-16 6 views
0

extrahieren Wie kann ich extrahieren Bytes aus double Typ. Ich weiß, es hat 8 Bytes, genau wie long. Wie kann ich eine long Variable erstellen, die die gleichen Bytes wie die double hat.C# Wie Bytes aus Doppel

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 :)) 

Wie kann ich das bitte schnell machen?

+2

Mögliche Duplikat von [Wie bekommen die Bits eines "doppelten" als "lang"] (https://stackoverflow.com/questions/4475611/how-to-get-the-bits -of-a-double-as-a-long) – harold

Antwort

2

Sie können es wie dieses

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

Es wird 4609434218613702656

sein als @harold

vorgeschlagen
var l2 = BitConverter.DoubleToInt64Bits(a); 

möglich ist zu

+0

BitConverter hat auch DoubleToInt64Bits – harold