英語で読む 編集

次の方法で共有


BitConverter.ToInt32 Method

Definition

Overloads

ToInt32(ReadOnlySpan<Byte>)

Converts a read-only byte span into a 32-bit signed integer.

ToInt32(Byte[], Int32)

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

ToInt32(ReadOnlySpan<Byte>)

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

Converts a read-only byte span into a 32-bit signed integer.

public static int ToInt32(ReadOnlySpan<byte> value);

Parameters

value
ReadOnlySpan<Byte>

A read-only span containing the bytes to convert.

Returns

A 32-bit signed integer representing the converted bytes.

Exceptions

The length of value is less than 4.

Applies to

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

ToInt32(Byte[], Int32)

Source:
BitConverter.cs
Source:
BitConverter.cs
Source:
BitConverter.cs

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

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

Parameters

value
Byte[]

An array of bytes that includes the four bytes to convert.

startIndex
Int32

The starting position within value.

Returns

A 32-bit signed integer formed by four bytes beginning at startIndex.

Exceptions

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.

value is null.

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

Examples

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)

Remarks

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.

See also

Applies to

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0