Converting a Big Endian Integer to Low Endian using C#

I ran into a mildly interesting problem recently. I use C# as my primary programming language. I was reading a binary file where integer values were stored in Big Endian (non-Intel) form, and because I use standard PC machines, I needed to convert the bytes read into Little Endian (Intel) form. As a minor side problem I wanted to be able to print these integer values as binary strings so I could see what was going on.

For example, the integer value 2051 when stored in Big Endian (non-Intel) form is:

00000011 00001000 00000000 00000000

But 2051 when stored in Little Endian (Intel) form is:

00000000 00000000 00001000 00000011

Notice the Little Endian is much easier to interpret by eye by reading from right to left. The value is 1 + 2 + (no 4s, no 8s, etc.) + 2048 = 2051.

Also notice that it is the 4 bytes which need to be reversed, not the entire 32-bit sequence of binary digits. There are two general approaches you can take for reversing bytes. You can use bit shift and logical operators, or use high level C# methods.

Here’s a bit manipulation approach:

static int ReverseBytes(int val)
  return (val & 0x000000FF) << 24 |
          (val & 0x0000FF00) << 8 |
          (val & 0x00FF0000) >> 8 |
          ((int)(val & 0xFF000000)) >> 24;

And here’s an example of the high level approach:

static int ReverseBytes(int val)
  byte[] intAsBytes = BitConverter.GetBytes(val);
  return BitConverter.ToInt32(intAsBytes, 0);

In the old days when I did lots of bit manipulation and coded using the C language, I preferred the bit manipulation approach, but now I generally prefer the high level approach.

Here’s a helper method I use to display an integer value as a binary string, with a blank space every 8 bits for readability:

static string IntToBinaryString(int v)
  string s = Convert.ToString(v, 2); // base 2
  string t = s.PadLeft(32, '0'); // add leading 0s
  string res = ""; // result
  for (int i = 0; i < t.Length; ++i)
    if (i > 0 && i % 8 == 0)
      res += " "; // add spaces
    res += t[i];
  return res;

And here’s a short demo C# program where I start with a Little Endian value, convert it to Big Endian, and then convert that result back to Little Endian.

static void Main(string[] args)
  Console.WriteLine("Begin change endian demo");

  int vLittle = 2051; // Intel, little endian
  int vBig = ReverseBytes(vLittle);

  string sLittle = IntToBinaryString(vLittle);
  string sBig = IntToBinaryString(vBig);

  int oLittle = ReverseBytes(vBig); // original value?
  string oString = IntToBinaryString(oLittle);

  Console.WriteLine("Original (Intel) little endian value = "
    + vLittle);
  Console.WriteLine("Original value as binary string = "
    + sLittle);
  Console.WriteLine("Reversed (non-Intel) big endian value = "
    + vBig);
  Console.WriteLine("Reversed (non-Intel) value as string = "
    + sBig);
  Console.WriteLine("The reversed big endian value = "
    + oLittle);
  Console.WriteLine("The reversed big endian value as string = "
    + oString);

  Console.WriteLine("End change endian demo");

Anyway, I can’t say this was one of the most interesting problems I’ve ever looked at, but the problem was pretty entertaining.


This entry was posted in Machine Learning, Software Test Automation. Bookmark the permalink.