C# Program to Convert Big Endian to Little Endian
Posted by Superadmin on August 11 2022 08:16:49

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);
            Array.Reverse(intAsBytes);
            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("");
            Console.WriteLine("Reversed big endian value = "
              + big);
            Console.WriteLine("Reversed value as string = "
              + sBig);
            Console.WriteLine("");
            Console.ReadLine();
        }
    }
}
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.

 

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.

 
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