raydenl raydenl - 1 month ago 19
C# Question

Convert Double to Byte*

I need some C# code to convert a double to a byte*. I know I have to use fixed (and unsafe?), but not exactly sure how...

I have seen a few other examples on here, but none quite the same.

Thanks in advance!

Cheers

Answer

You can do:

unsafe
{
    fixed (byte* b = BitConverter.GetBytes(1.2d))
    {
        // Do stuff...
    }
}

or :

public unsafe void YourMethod(double d)
{
    fixed (byte* b = BitConverter.GetBytes(d))
    {
        // Do stuff...
    }
}