none
Convert double datatype from Little Endian to Big Endian in c# RRS feed

  • Question

  •  

    How do I convert double datatype from Little Endian to Big Endian and vice versa in c#.

    Any help will be highly appreciated.

     

    Regards,

    Sidharth Shah

     

    Tuesday, May 13, 2008 8:23 AM

Answers

  • Well, you can use BitConverter to get to/from the underlying byte representation, but this will be in the machine's endianness - almost always little endian (except for itaniums).

     

    If you have the byte[], you can just reverse it? Some subset of:

     

    Code Snippet

    double value = Math.PI;

    byte[] buffer = BitConverter.GetBytes(value);

    Array.Reverse(buffer);

    double newValue = BitConverter.ToDouble(buffer, 0);

     

    (yes the above is a daft thing to do - just for illustration)

     

    Note that Jon Skeet has an EndianBitConverter here that might be of use.

    Tuesday, May 13, 2008 10:11 AM