donaastor donaastor - 6 months ago 59
C# Question

C# How to extract bytes from double

How can I extract bytes from

double
type. I know it has 8 bytes, just like
long
. How can I create a
long
variable that has the same bytes as the
double
has.

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


How can I do this quickly, please?

L.B L.B
Answer Source

You can get it like this

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

It will be 4609434218613702656

As @harold suggested

var l2 = BitConverter.DoubleToInt64Bits(a);

is possible too

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download