C# Program to Convert Big Endian to Little Endian


This is a C# Program to convert big endian to little endian.

Problem Description

This C# Program Converts Big Endian to Little Endian.

Problem Solution

Here the big endian value is converted to little endian value.

Program/Source Code

Here is source code of the C# Program to Convert Big Endian to Little Endian. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 *  C# Program to Convert Big Endian to Little Endian
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
    class Program
        static int ReverseBytes(int val)
            byte[] intAsBytes = BitConverter.GetBytes(val);
            return BitConverter.ToInt32(intAsBytes, 0);
        static string IntToBinaryString(int v)
            string s = Convert.ToString(v, 2); 
            string t = s.PadLeft(32, '0'); 
            string res = ""; 
            for (int i = 0; i < t.Length; ++i)
                if (i > 0 && i % 8 == 0)
                    res += " "; 
                res += t[i];
            return res;
        static void Main(string[] args)
            int little = 2777; 
            int big = ReverseBytes(little);
            string sLittle = IntToBinaryString(little);
            string sBig = IntToBinaryString(big);
            int oLittle = ReverseBytes(big);
            string oString = IntToBinaryString(oLittle);
            Console.WriteLine("Original (Intel) little endian value = "
              + little);
            Console.WriteLine("Original value as binary string = "
              + sLittle);
            Console.WriteLine("Reversed big endian value = "
              + big);
            Console.WriteLine("Reversed value as string = "
              + sBig);
Program Explanation

This C# program is used to convert big endian to little endian. We have defined the value of ‘little’ variable as 2777. The BitConverter is used to convert the given integer value to bytes and reverse the value using Reverse() function.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

The IntToBinaryString() function, is used to order of the bytes in the array returned by the GetBytes method overloads, (as well as the order of bits in the integer returned by the DoubleToInt64Bits method and the order of hexadecimal strings returned by the ToString( Byte[] ) method) depends on whether the computer architecture is little-endian or big-endian.

Similarly, the order of bytes in the array and returned by the ToIntegerValue methods and the ToChar method depends on whether the computer architecture is little-endian or big-endian. The endianness of architecture is indicated by the IsLittleEndian property, which returns true on little-endian systems and false on big-endian systems.

On little-endian systems, lower-order bytes precede higher-order bytes. On big-endian system, higher-order bytes precede lower-order bytes. The bytes are listed in order from the byte at index 0 to the byte at index 3.

Check this: MCA Books | C# Books
Runtime Test Cases
Original (Intel) little endian value = 2777
Original value as binary string = 00000000 00000000 00001010 11011001
Reversed big endian value = -653656064
Reversed value as string = 11011001 00001010 00000000 00000000

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.