donaastor donaastor -3 years ago 232
C# Question

C# How to extract bytes from double

How can I extract bytes from

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

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?

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