Problem
Determine the endianess of a computer
Solution
using System;
using System.Runtime.InteropServices;
namespace Endianess
{
[StructLayout(LayoutKind.Explicit)]
struct Union
{
[FieldOffset(0)]
public int i;
[FieldOffset(0)]
public byte b;
}
class Program
{
static bool IsLittleEndian1()
{
Union u;
u.b = 0;
u.i = 1;
return (u.b == 1) ? true : false;
}
static bool IsLittleEndian2()
{
return BitConverter.IsLittleEndian;
}
static void Main(string[] args)
{
if (Program.IsLittleEndian1())
{
Console.WriteLine("Little Endianess - Method 1");
}
else
{
Console.WriteLine("Little Endianess - Method 1");
}
if (Program.IsLittleEndian2())
{
Console.WriteLine("Little Endianess - Method 2");
}
else
{
Console.WriteLine("Little Endianess - Method 2");
}
}
}
}
Output
Little Endianess - Method 1
Little Endianess - Method 2
Press any key to continue . . .