-1

For Example :

I am trying to do this: 0x64CEED7E becomes 7EEDCE64.

This is my code.

for (int integerTemp = 0; integerTemp < 4; integerTemp++)
{
   generatedKey[integerTemp] = Convert.ToByte(((uint)(integerkey & (0x000000FF   << (integerTemp * 8)))) >> (integerTemp * 8));
}

What is the best way of doing this ?

  • Possible duplicate of [Bitwise endian swap for various types](http://stackoverflow.com/questions/19560436/bitwise-endian-swap-for-various-types) – harold May 20 '16 at 20:16
  • You can use this : static uint Convert(uint input) { return ((input & 0x000000ffU) << 24) + ((input & 0x0000ff00U) << 8) + ((input & 0x00ff0000U) >> 8) + ((input & 0xff000000U) >> 24); } – jdweng May 20 '16 at 21:52

1 Answers1

1

You can also use the BitConverter class:

int key = 0x64CEED7E;
var bytes = BitConverter.GetBytes(key);
Array.Reverse(bytes);
key = BitConverter.ToInt32(bytes, 0);

Console.WriteLine(key.ToString("x"));
kagelos
  • 423
  • 8
  • 19