# C# Reversing the bytes of a 64 Bit Hex Value

• ### Question

• Hello Everyone,

I have a 64 bit hex value (0x0000C0001CD640C8) and I want to reverse its bytes and get the result as 0x40C81CD6C0000000  in C#

Finally I want to convert this to a double value and get 12345,678 value.

What is the simplest way of doing this please ..

Best Regards

• Edited by Thursday, July 27, 2017 2:24 PM
Thursday, July 27, 2017 2:23 PM

### All replies

• Try this relatively simple way:

```Int64 n = 0x0000C0001CD640C8;
byte[] b1 = BitConverter.GetBytes( n );
byte[] b2 = new byte[8];
for( int i = 0; i < 8; ++i ) b2[i + 6 - 4 * ( i / 2 )] = b1[i];
double d = BitConverter.ToDouble( b2, 0 );
Console.WriteLine( d ); // 12345,677734375 ```

Can be done with a single array too.

• Edited by Thursday, July 27, 2017 4:49 PM
Thursday, July 27, 2017 4:46 PM
• Hello @Viorel_

First of all thank you for your answer. Your reply was ok for the HEX value i wrote above. But when I change the hex value for example 0x40C8  your code gives result 12288 while the decimal represtantation of the HEX 0xC840 is 51264 ?

I know the value changed from 64 bit to 16 bit, maybe the issue is because of this. But how can we produce a universal solution for all 16 32 64 bit values.

Regards

Thursday, July 27, 2017 7:41 PM
• But 0xC840 is integer not double right? The double value for 51264 should be 0x08E940.

Try byte[] b1 = new byte[] { 0xE9, 0x40, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 };

Friday, July 28, 2017 2:37 AM
• Hi kfalus,

The following code for 64bit Hex value for your reference.

```using System;
using System.Globalization;
using System.Runtime.InteropServices;

namespace HexValueConvert
{
[StructLayout(LayoutKind.Explicit)]
struct Double2ulong
{
[FieldOffset(0)]
public double d;
[FieldOffset(0)]
public ulong ul;
}
class Program
{
static void Main(string[] args)
{
ulong hex64 = 0x0000C0001CD640C8;

ulong word1 = (hex64 & 0xFFFF) << 48;
ulong word2 = (hex64 & 0xFFFF0000) << 16;
ulong word3 = (hex64 & 0xFFFF00000000) >> 16;
ulong word4 = (hex64 & 0xFFFF000000000000) >> 48;
ulong words = word1 | word2 | word3 | word4;
Console.WriteLine("{0:X16}", words);
ulong parsed = ulong.Parse(words.ToString("X16"), NumberStyles.AllowHexSpecifier);

Double2ulong d2ul = new Double2ulong();

d2ul.ul = parsed;

Console.WriteLine(d2ul.d.ToString());

}
}
}
```

Best regards,

Cole Wu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Friday, July 28, 2017 6:56 AM