Export (0) Print
Expand All

BitConverter.ToInt32 Method

Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static int ToInt32(
	byte[] value,
	int startIndex
)

Parameters

value
Type: System.Byte[]

An array of bytes.

startIndex
Type: System.Int32

The starting position within value.

Return Value

Type: System.Int32
A 32-bit signed integer formed by four bytes beginning at startIndex.

ExceptionCondition
ArgumentException

startIndex is greater than or equal to the length of value minus 3, and is less than or equal to the length of value minus 1.

ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex is less than zero or greater than the length of value minus 1.

The ToInt32 method converts the bytes from index startIndex to startIndex + 3 to an Int32 value. The order of bytes in the array must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of BitConverter.

The following example uses the ToInt32 method to create Int32 values from a four-byte array and from the upper four bytes of an eight-byte array. It also uses the GetBytes(Int32) and ToInt32 methods to round-trip an Int32 value.

using System;

public class Example
{
   public static void Main()
   {
      // Create an Integer from a 4-byte array.
      Byte[] bytes1 = { 0xEC, 0x00, 0x00, 0x00 };
      Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes1),
                                      BitConverter.ToInt32(bytes1, 0));
      // Create an Integer from the upper four bytes of a byte array.
      Byte[] bytes2 = BitConverter.GetBytes(Int64.MaxValue / 2);
      Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes2),
                                      BitConverter.ToInt32(bytes2, 4));

      // Round-trip an integer value. 
      int original = (int) Math.Pow(16, 3);
      Byte[] bytes3 = BitConverter.GetBytes(original);
      int restored = BitConverter.ToInt32(bytes3, 0);
      Console.WriteLine("0x{0:X4} ({0:N0}) --> {1} --> 0x{2:X4} ({2:N0})", original, 
                        FormatBytes(bytes3), restored);
   }

   private static string FormatBytes(Byte[] bytes)
   {
       string value = "";
       foreach (var byt in bytes)
          value += String.Format("{0:X2} ", byt);

       return value;
   }
}
// The example displays the following output: 
//       EC 00 00 00 --> 0x00EC (236) 
//       FF FF FF FF FF FF FF 3F --> 0x3FFFFFFF (1,073,741,823) 
//       0x1000 (4,096) --> 00 10 00 00  --> 0x1000 (4,096)

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft