Convert Byte Array to Int in C#

The BitConverter class in .NET Framework is provides functionality to convert base data types to an array of bytes, and an array of bytes to base data types.
 
The BitConverter class has a static overloaded GetBytes method that takes an integer, double or other base type value and convert that to a array of bytes. The BitConverter class also have other static methods to reverse this conversion. Some of these methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.
 
The following code snippet converts different integer values to a byte array and vice-versa.
  1. namespace BitConverterSample  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("Int and byte arrays conversion sample.");  
  8.             // Create double to a byte array  
  9.             Int32 i32 = 125;  
  10.             Console.WriteLine("Int value: " + i32.ToString());  
  11.             byte[] bytes = ConvertInt32ToByteArray(i32);  
  12.             Console.WriteLine("Byte array value:");  
  13.             Console.WriteLine(BitConverter.ToString(bytes));  
  14.             Console.WriteLine("Byte array back to Int32:");  
  15.             // Create byte array to Int32  
  16.             double dValue = ConvertByteArrayToInt32(bytes);  
  17.             Console.WriteLine(dValue.ToString());  
  18.             Console.ReadLine();  
  19.         }  
  20.         public static byte[] ConvertInt32ToByteArray(Int32 I32)  
  21.         {  
  22.             return BitConverter.GetBytes(I32);  
  23.         }  
  24.         public static byte[] ConvertIntToByteArray(Int16 I16)  
  25.         {  
  26.             return BitConverter.GetBytes(I16);  
  27.         }  
  28.         public static byte[] ConvertIntToByteArray(Int64 I64)  
  29.         {  
  30.             return BitConverter.GetBytes(I64);  
  31.         }  
  32.         public static byte[] ConvertIntToByteArray(int I)  
  33.         {  
  34.             return BitConverter.GetBytes(I);  
  35.         }  
  36.         public static double ConvertByteArrayToInt32(byte[] b)  
  37.         {  
  38.             return BitConverter.ToInt32(b, 0);  
  39.         }  
  40.     }