Int32 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
32 ビット符号付き整数を表します。
public value class int : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public value class int : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, IUtf8SpanParsable<int>, System::Numerics::IAdditionOperators<int, int, int>, System::Numerics::IAdditiveIdentity<int, int>, System::Numerics::IBinaryInteger<int>, System::Numerics::IBinaryNumber<int>, System::Numerics::IBitwiseOperators<int, int, int>, System::Numerics::IComparisonOperators<int, int, bool>, System::Numerics::IDecrementOperators<int>, System::Numerics::IDivisionOperators<int, int, int>, System::Numerics::IEqualityOperators<int, int, bool>, System::Numerics::IIncrementOperators<int>, System::Numerics::IMinMaxValue<int>, System::Numerics::IModulusOperators<int, int, int>, System::Numerics::IMultiplicativeIdentity<int, int>, System::Numerics::IMultiplyOperators<int, int, int>, System::Numerics::INumber<int>, System::Numerics::INumberBase<int>, System::Numerics::IShiftOperators<int, int, int>, System::Numerics::ISignedNumber<int>, System::Numerics::ISubtractionOperators<int, int, int>, System::Numerics::IUnaryNegationOperators<int, int>, System::Numerics::IUnaryPlusOperators<int, int>
public value class int : IComparable, IComparable<int>, IConvertible, IEquatable<int>, ISpanFormattable
public value class int : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, System::Numerics::IAdditionOperators<int, int, int>, System::Numerics::IAdditiveIdentity<int, int>, System::Numerics::IBinaryInteger<int>, System::Numerics::IBinaryNumber<int>, System::Numerics::IBitwiseOperators<int, int, int>, System::Numerics::IComparisonOperators<int, int, bool>, System::Numerics::IDecrementOperators<int>, System::Numerics::IDivisionOperators<int, int, int>, System::Numerics::IEqualityOperators<int, int, bool>, System::Numerics::IIncrementOperators<int>, System::Numerics::IMinMaxValue<int>, System::Numerics::IModulusOperators<int, int, int>, System::Numerics::IMultiplicativeIdentity<int, int>, System::Numerics::IMultiplyOperators<int, int, int>, System::Numerics::INumber<int>, System::Numerics::INumberBase<int>, System::Numerics::IShiftOperators<int, int, int>, System::Numerics::ISignedNumber<int>, System::Numerics::ISubtractionOperators<int, int, int>, System::Numerics::IUnaryNegationOperators<int, int>, System::Numerics::IUnaryPlusOperators<int, int>
public value class int : IComparable, IConvertible, IFormattable
public value class int : IComparable, IComparable<int>, IEquatable<int>, IFormattable
public struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public readonly struct Int32 : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, IUtf8SpanParsable<int>, System.Numerics.IAdditionOperators<int,int,int>, System.Numerics.IAdditiveIdentity<int,int>, System.Numerics.IBinaryInteger<int>, System.Numerics.IBinaryNumber<int>, System.Numerics.IBitwiseOperators<int,int,int>, System.Numerics.IComparisonOperators<int,int,bool>, System.Numerics.IDecrementOperators<int>, System.Numerics.IDivisionOperators<int,int,int>, System.Numerics.IEqualityOperators<int,int,bool>, System.Numerics.IIncrementOperators<int>, System.Numerics.IMinMaxValue<int>, System.Numerics.IModulusOperators<int,int,int>, System.Numerics.IMultiplicativeIdentity<int,int>, System.Numerics.IMultiplyOperators<int,int,int>, System.Numerics.INumber<int>, System.Numerics.INumberBase<int>, System.Numerics.IShiftOperators<int,int,int>, System.Numerics.ISignedNumber<int>, System.Numerics.ISubtractionOperators<int,int,int>, System.Numerics.IUnaryNegationOperators<int,int>, System.Numerics.IUnaryPlusOperators<int,int>
public readonly struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public readonly struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, ISpanFormattable
public readonly struct Int32 : IComparable<int>, IConvertible, IEquatable<int>, IParsable<int>, ISpanParsable<int>, System.Numerics.IAdditionOperators<int,int,int>, System.Numerics.IAdditiveIdentity<int,int>, System.Numerics.IBinaryInteger<int>, System.Numerics.IBinaryNumber<int>, System.Numerics.IBitwiseOperators<int,int,int>, System.Numerics.IComparisonOperators<int,int,bool>, System.Numerics.IDecrementOperators<int>, System.Numerics.IDivisionOperators<int,int,int>, System.Numerics.IEqualityOperators<int,int,bool>, System.Numerics.IIncrementOperators<int>, System.Numerics.IMinMaxValue<int>, System.Numerics.IModulusOperators<int,int,int>, System.Numerics.IMultiplicativeIdentity<int,int>, System.Numerics.IMultiplyOperators<int,int,int>, System.Numerics.INumber<int>, System.Numerics.INumberBase<int>, System.Numerics.IShiftOperators<int,int,int>, System.Numerics.ISignedNumber<int>, System.Numerics.ISubtractionOperators<int,int,int>, System.Numerics.IUnaryNegationOperators<int,int>, System.Numerics.IUnaryPlusOperators<int,int>
[System.Serializable]
public struct Int32 : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Int32 : IComparable, IComparable<int>, IConvertible, IEquatable<int>, IFormattable
public struct Int32 : IComparable, IComparable<int>, IEquatable<int>, IFormattable
type int = struct
interface IConvertible
interface IFormattable
type int = struct
interface IConvertible
interface IFormattable
interface IParsable<int>
interface ISpanFormattable
interface ISpanParsable<int>
interface IUtf8SpanFormattable
interface IUtf8SpanParsable<int>
interface IAdditionOperators<int, int, int>
interface IAdditiveIdentity<int, int>
interface IBinaryInteger<int>
interface IBinaryNumber<int>
interface IBitwiseOperators<int, int, int>
interface IComparisonOperators<int, int, bool>
interface IEqualityOperators<int, int, bool>
interface IDecrementOperators<int>
interface IDivisionOperators<int, int, int>
interface IIncrementOperators<int>
interface IModulusOperators<int, int, int>
interface IMultiplicativeIdentity<int, int>
interface IMultiplyOperators<int, int, int>
interface INumber<int>
interface INumberBase<int>
interface ISubtractionOperators<int, int, int>
interface IUnaryNegationOperators<int, int>
interface IUnaryPlusOperators<int, int>
interface IShiftOperators<int, int, int>
interface IMinMaxValue<int>
interface ISignedNumber<int>
type int = struct
interface IConvertible
interface ISpanFormattable
interface IFormattable
type int = struct
interface IConvertible
interface IFormattable
interface IParsable<int>
interface ISpanFormattable
interface ISpanParsable<int>
interface IAdditionOperators<int, int, int>
interface IAdditiveIdentity<int, int>
interface IBinaryInteger<int>
interface IBinaryNumber<int>
interface IBitwiseOperators<int, int, int>
interface IComparisonOperators<int, int, bool>
interface IEqualityOperators<int, int, bool>
interface IDecrementOperators<int>
interface IDivisionOperators<int, int, int>
interface IIncrementOperators<int>
interface IModulusOperators<int, int, int>
interface IMultiplicativeIdentity<int, int>
interface IMultiplyOperators<int, int, int>
interface INumber<int>
interface INumberBase<int>
interface ISubtractionOperators<int, int, int>
interface IUnaryNegationOperators<int, int>
interface IUnaryPlusOperators<int, int>
interface IShiftOperators<int, int, int>
interface IMinMaxValue<int>
interface ISignedNumber<int>
type int = struct
interface IConvertible
interface IFormattable
interface IParsable<int>
interface ISpanFormattable
interface ISpanParsable<int>
interface IAdditionOperators<int, int, int>
interface IAdditiveIdentity<int, int>
interface IBinaryInteger<int>
interface IBinaryNumber<int>
interface IBitwiseOperators<int, int, int>
interface IComparisonOperators<int, int, bool>
interface IEqualityOperators<int, int, bool>
interface IDecrementOperators<int>
interface IDivisionOperators<int, int, int>
interface IIncrementOperators<int>
interface IModulusOperators<int, int, int>
interface IMultiplicativeIdentity<int, int>
interface IMultiplyOperators<int, int, int>
interface INumber<int>
interface INumberBase<int>
interface ISubtractionOperators<int, int, int>
interface IUnaryNegationOperators<int, int>
interface IUnaryPlusOperators<int, int>
interface IUtf8SpanFormattable
interface IUtf8SpanParsable<int>
interface IShiftOperators<int, int, int>
interface IMinMaxValue<int>
interface ISignedNumber<int>
[<System.Serializable>]
type int = struct
interface IFormattable
interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type int = struct
interface IFormattable
interface IConvertible
type int = struct
interface IFormattable
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IConvertible, IEquatable(Of Integer), IFormattable
Public Structure Int32
Implements IAdditionOperators(Of Integer, Integer, Integer), IAdditiveIdentity(Of Integer, Integer), IBinaryInteger(Of Integer), IBinaryNumber(Of Integer), IBitwiseOperators(Of Integer, Integer, Integer), IComparable(Of Integer), IComparisonOperators(Of Integer, Integer, Boolean), IConvertible, IDecrementOperators(Of Integer), IDivisionOperators(Of Integer, Integer, Integer), IEqualityOperators(Of Integer, Integer, Boolean), IEquatable(Of Integer), IIncrementOperators(Of Integer), IMinMaxValue(Of Integer), IModulusOperators(Of Integer, Integer, Integer), IMultiplicativeIdentity(Of Integer, Integer), IMultiplyOperators(Of Integer, Integer, Integer), INumber(Of Integer), INumberBase(Of Integer), IParsable(Of Integer), IShiftOperators(Of Integer, Integer, Integer), ISignedNumber(Of Integer), ISpanParsable(Of Integer), ISubtractionOperators(Of Integer, Integer, Integer), IUnaryNegationOperators(Of Integer, Integer), IUnaryPlusOperators(Of Integer, Integer), IUtf8SpanParsable(Of Integer)
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IConvertible, IEquatable(Of Integer), ISpanFormattable
Public Structure Int32
Implements IAdditionOperators(Of Integer, Integer, Integer), IAdditiveIdentity(Of Integer, Integer), IBinaryInteger(Of Integer), IBinaryNumber(Of Integer), IBitwiseOperators(Of Integer, Integer, Integer), IComparable(Of Integer), IComparisonOperators(Of Integer, Integer, Boolean), IConvertible, IDecrementOperators(Of Integer), IDivisionOperators(Of Integer, Integer, Integer), IEqualityOperators(Of Integer, Integer, Boolean), IEquatable(Of Integer), IIncrementOperators(Of Integer), IMinMaxValue(Of Integer), IModulusOperators(Of Integer, Integer, Integer), IMultiplicativeIdentity(Of Integer, Integer), IMultiplyOperators(Of Integer, Integer, Integer), INumber(Of Integer), INumberBase(Of Integer), IParsable(Of Integer), IShiftOperators(Of Integer, Integer, Integer), ISignedNumber(Of Integer), ISpanParsable(Of Integer), ISubtractionOperators(Of Integer, Integer, Integer), IUnaryNegationOperators(Of Integer, Integer), IUnaryPlusOperators(Of Integer, Integer)
Public Structure Int32
Implements IComparable, IConvertible, IFormattable
Public Structure Int32
Implements IComparable, IComparable(Of Integer), IEquatable(Of Integer), IFormattable
- 継承
- 属性
- 実装
-
IComparable IComparable<Int32> IConvertible IEquatable<Int32> IFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Int32> IParsable<TSelf> ISpanFormattable ISpanParsable<Int32> ISpanParsable<TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Int32> IUtf8SpanParsable<TSelf> IAdditionOperators<Int32,Int32,Int32> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Int32,Int32> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<Int32> IBinaryNumber<Int32> IBinaryNumber<TSelf> IBitwiseOperators<Int32,Int32,Int32> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Int32,Int32,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Int32> IDecrementOperators<TSelf> IDivisionOperators<Int32,Int32,Int32> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Int32,Int32,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IIncrementOperators<Int32> IIncrementOperators<TSelf> IMinMaxValue<Int32> IModulusOperators<Int32,Int32,Int32> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Int32,Int32> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Int32,Int32,Int32> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Int32> INumber<TSelf> INumberBase<Int32> INumberBase<TSelf> IShiftOperators<Int32,Int32,Int32> IShiftOperators<TSelf,Int32,TSelf> ISignedNumber<Int32> ISubtractionOperators<Int32,Int32,Int32> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<Int32,Int32> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Int32,Int32> IUnaryPlusOperators<TSelf,TSelf>
注釈
Int32 は、負の 2,147,483,648 ( Int32.MinValue 定数で表される) から正の 2,147,483,647 ( Int32.MaxValue 定数で表される) までの範囲の符号付き整数を表す不変の値型です。 .NET には、0 ~ 4,294,967,295 の範囲の値を表す、符号なし 32 ビット整数値型の UInt32も含まれています。
Int32 値をインスタンス化する
Int32値は、いくつかの方法でインスタンス化できます。
Int32変数を宣言し、Int32データ型の範囲内にあるリテラル整数値を割り当てることができます。 次の例では、2 つの Int32 変数を宣言し、この方法で値を割り当てます。
int number1 = 64301; int number2 = 25548612;let number1 = 64301 let number2 = 25548612Dim number1 As Integer = 64301 Dim number2 As Integer = 25548612範囲が Int32 型のサブセットである整数型の値を割り当てることができます。 これは拡大変換であり、C# のキャスト演算子や Visual Basic の変換メソッドは必要ありませんが、F# では必要です。
sbyte value1 = 124; short value2 = 1618; int number1 = value1; int number2 = value2;let value1 = 124y let value2 = 1618s let number1 = int value1 let number2 = int value2Dim value1 As SByte = 124 Dim value2 As Int16 = 1618 Dim number1 As Integer = value1 Dim number2 As Integer = value2範囲が Int32 型の値を超える数値型の値を割り当てることができます。 これは縮小変換であるため、C# または F# のキャスト演算子と、
Option Strictがオンの場合は Visual Basic の変換メソッドが必要です。 数値が小数部を含む Single、 Double、または Decimal 値の場合、その小数部の処理は、変換を実行するコンパイラによって異なります。 次の例では、縮小変換を実行して、複数の数値を Int32 変数に割り当てます。long lNumber = 163245617; try { int number1 = (int) lNumber; Console.WriteLine(number1); } catch (OverflowException) { Console.WriteLine($"{lNumber} is out of range of an Int32."); } double dbl2 = 35901.997; try { int number2 = (int) dbl2; Console.WriteLine(number2); } catch (OverflowException) { Console.WriteLine($"{dbl2} is out of range of an Int32."); } BigInteger bigNumber = 132451; try { int number3 = (int) bigNumber; Console.WriteLine(number3); } catch (OverflowException) { Console.WriteLine($"{bigNumber} is out of range of an Int32."); } // The example displays the following output: // 163245617 // 35902 // 132451let lNumber = 163245617L try let number1 = int lNumber printfn $"{number1}" with :? OverflowException -> printfn $"{lNumber} is out of range of an Int32." let dbl2 = 35901.997 try let number2 = int dbl2 printfn $"{number2}" with :? OverflowException -> printfn $"{dbl2} is out of range of an Int32." let bigNumber = BigInteger 132451 try let number3 = int bigNumber printfn $"{number3}" with :? OverflowException -> printfn $"{bigNumber} is out of range of an Int32." // The example displays the following output: // 163245617 // 35902 // 132451Dim lNumber As Long = 163245617 Try Dim number1 As Integer = CInt(lNumber) Console.WriteLine(number1) Catch e As OverflowException Console.WriteLine("{0} is out of range of an Int32.", lNumber) End Try Dim dbl2 As Double = 35901.997 Try Dim number2 As Integer = CInt(dbl2) Console.WriteLine(number2) Catch e As OverflowException Console.WriteLine("{0} is out of range of an Int32.", dbl2) End Try Dim bigNumber As BigInteger = 132451 Try Dim number3 As Integer = CInt(bigNumber) Console.WriteLine(number3) Catch e As OverflowException Console.WriteLine("{0} is out of range of an Int32.", bigNumber) End Try ' The example displays the following output: ' 163245617 ' 35902 ' 132451Convert クラスのメソッドを呼び出して、サポートされている任意の型をInt32値に変換できます。 これは、 Int32 が IConvertible インターフェイスをサポートしているためです。 次の例は、 Decimal 値の配列から Int32 値への変換を示しています。
decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, 199.55m, 9214.16m, Decimal.MaxValue }; int result; foreach (decimal value in values) { try { result = Convert.ToInt32(value); Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", value.GetType().Name, value, result.GetType().Name, result); } catch (OverflowException) { Console.WriteLine("{0} is outside the range of the Int32 type.", value); } } // The example displays the following output: // -79228162514264337593543950335 is outside the range of the Int32 type. // Converted the Decimal value '-1034.23' to the Int32 value -1034. // Converted the Decimal value '-12' to the Int32 value -12. // Converted the Decimal value '0' to the Int32 value 0. // Converted the Decimal value '147' to the Int32 value 147. // Converted the Decimal value '199.55' to the Int32 value 200. // Converted the Decimal value '9214.16' to the Int32 value 9214. // 79228162514264337593543950335 is outside the range of the Int32 type.let values = [| Decimal.MinValue; -1034.23M; -12m; 0M; 147M 199.55M; 9214.16M; Decimal.MaxValue |] for value in values do try let result = Convert.ToInt32 value printfn $"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}." with :? OverflowException -> printfn $"{value} is outside the range of the Int32 type." // The example displays the following output: // -79228162514264337593543950335 is outside the range of the Int32 type. // Converted the Decimal value '-1034.23' to the Int32 value -1034. // Converted the Decimal value '-12' to the Int32 value -12. // Converted the Decimal value '0' to the Int32 value 0. // Converted the Decimal value '147' to the Int32 value 147. // Converted the Decimal value '199.55' to the Int32 value 200. // Converted the Decimal value '9214.16' to the Int32 value 9214. // 79228162514264337593543950335 is outside the range of the Int32 type.Dim values() As Decimal = { Decimal.MinValue, -1034.23d, -12d, 0d, 147d, _ 199.55d, 9214.16d, Decimal.MaxValue } Dim result As Integer For Each value As Decimal In values Try result = Convert.ToInt32(value) Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _ value.GetType().Name, value, _ result.GetType().Name, result) Catch e As OverflowException Console.WriteLine("{0} is outside the range of the Int32 type.", _ value) End Try Next ' The example displays the following output: ' -79228162514264337593543950335 is outside the range of the Int32 type. ' Converted the Decimal value '-1034.23' to the Int32 value -1034. ' Converted the Decimal value '-12' to the Int32 value -12. ' Converted the Decimal value '0' to the Int32 value 0. ' Converted the Decimal value '147' to the Int32 value 147. ' Converted the Decimal value '199.55' to the Int32 value 200. ' Converted the Decimal value '9214.16' to the Int32 value 9214. ' 79228162514264337593543950335 is outside the range of the Int32 type.ParseまたはTryParseメソッドを呼び出して、Int32値の文字列形式をInt32に変換できます。 文字列には、10 進数または 16 進数を含めることができます。 次の例は、10 進文字列と 16 進文字列の両方を使用した解析操作を示しています。
string string1 = "244681"; try { int number1 = Int32.Parse(string1); Console.WriteLine(number1); } catch (OverflowException) { Console.WriteLine($"'{string1}' is out of range of a 32-bit integer."); } catch (FormatException) { Console.WriteLine($"The format of '{string1}' is invalid."); } string string2 = "F9A3C"; try { int number2 = Int32.Parse(string2, System.Globalization.NumberStyles.HexNumber); Console.WriteLine(number2); } catch (OverflowException) { Console.WriteLine($"'{string2}' is out of range of a 32-bit integer."); } catch (FormatException) { Console.WriteLine($"The format of '{string2}' is invalid."); } // The example displays the following output: // 244681 // 1022524let string1 = "244681" try let number1 = Int32.Parse string1 printfn $"{number1}" with | :? OverflowException -> printfn $"'{string1}' is out of range of a 32-bit integer." | :? FormatException -> printfn $"The format of '{string1}' is invalid." let string2 = "F9A3C" try let number2 = Int32.Parse(string2, System.Globalization.NumberStyles.HexNumber) printfn $"{number2}" with | :? OverflowException -> printfn $"'{string2}' is out of range of a 32-bit integer." | :? FormatException -> printfn $"The format of '{string2}' is invalid." // The example displays the following output: // 244681 // 1022524Dim string1 As String = "244681" Try Dim number1 As Integer = Int32.Parse(string1) Console.WriteLine(number1) Catch e As OverflowException Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string1) Catch e As FormatException Console.WriteLine("The format of '{0}' is invalid.", string1) End Try Dim string2 As String = "F9A3C" Try Dim number2 As Integer = Int32.Parse(string2, System.Globalization.NumberStyles.HexNumber) Console.WriteLine(number2) Catch e As OverflowException Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string2) Catch e As FormatException Console.WriteLine("The format of '{0}' is invalid.", string2) End Try ' The example displays the following output: ' 244681 ' 1022524
Int32 値に対して操作を実行する
Int32型は、加算、減算、除算、乗算、否定、単項否定などの標準的な数学演算をサポートします。 他の整数型と同様に、 Int32 型もビットごとの AND、 OR、 XOR、左シフト、および右シフト演算子をサポートします。
標準の数値演算子を使用して 2 つの Int32 値を比較したり、 CompareTo または Equals メソッドを呼び出したりできます。
また、 Math クラスのメンバーを呼び出して、数値の絶対値の取得、整数除算からの商と剰余の計算、2 つの整数の最大値または最小値の決定、数値の符号の取得、数値の丸めなど、さまざまな数値演算を実行することもできます。
Int32 を文字列として表す
Int32型は、標準およびカスタムの数値書式指定文字列を完全にサポートします。 (詳細については、「 書式設定の種類、 標準の数値書式指定文字列、 およびカスタム数値書式指定文字列」を参照してください)。
Int32値を、先頭にゼロがない整数文字列として書式設定するには、パラメーターなしのToString() メソッドを呼び出すことができます。 "D" 書式指定子を使用すると、文字列形式に指定した数の先行ゼロを含めることもできます。 "N" 書式指定子を使用すると、グループ区切り記号を含め、数値の文字列表現に表示する 10 進数の数を指定できます。 "X" 書式指定子を使用すると、 Int32 値を 16 進数の文字列として表すことができます。 次の例では、これらの 4 つの方法で Int32 値の配列内の要素を書式設定します。
int[] numbers = { -1403, 0, 169, 1483104 };
foreach (int number in numbers)
{
// Display value using default formatting.
Console.Write("{0,-8} --> ", number.ToString());
// Display value with 3 digits and leading zeros.
Console.Write("{0,11:D3}", number);
// Display value with 1 decimal digit.
Console.Write("{0,13:N1}", number);
// Display value as hexadecimal.
Console.Write("{0,12:X2}", number);
// Display value with eight hexadecimal digits.
Console.WriteLine("{0,14:X8}", number);
}
// The example displays the following output:
// -1403 --> -1403 -1,403.0 FFFFFA85 FFFFFA85
// 0 --> 000 0.0 00 00000000
// 169 --> 169 169.0 A9 000000A9
// 1483104 --> 1483104 1,483,104.0 16A160 0016A160
let numbers = [| -1403; 0; 169; 1483104 |]
for number in numbers do
// Display value using default formatting.
printf $"{number,-8} --> "
// Display value with 3 digits and leading zeros.
printf $"{number,11:D3}"
// Display value with 1 decimal digit.
printf $"{number,13:N1}"
// Display value as hexadecimal.
printf $"{number,12:X2}"
// Display value with eight hexadecimal digits.
printfn $"{number,14:X8}"
// The example displays the following output:
// -1403 --> -1403 -1,403.0 FFFFFA85 FFFFFA85
// 0 --> 000 0.0 00 00000000
// 169 --> 169 169.0 A9 000000A9
// 1483104 --> 1483104 1,483,104.0 16A160 0016A160
Dim numbers() As Integer = { -1403, 0, 169, 1483104 }
For Each number As Integer In numbers
' Display value using default formatting.
Console.Write("{0,-8} --> ", number.ToString())
' Display value with 3 digits and leading zeros.
Console.Write("{0,11:D3}", number)
' Display value with 1 decimal digit.
Console.Write("{0,13:N1}", number)
' Display value as hexadecimal.
Console.Write("{0,12:X2}", number)
' Display value with eight hexadecimal digits.
Console.WriteLine("{0,14:X8}", number)
Next
' The example displays the following output:
' -1403 --> -1403 -1,403.0 FFFFFA85 FFFFFA85
' 0 --> 000 0.0 00 00000000
' 169 --> 169 169.0 A9 000000A9
' 1483104 --> 1483104 1,483,104.0 16A160 0016A160
Int32値は、ToString(Int32, Int32) メソッドを呼び出し、メソッドの 2 番目のパラメーターとして base を指定することで、バイナリ、8 進数、10 進数、または 16 進数の文字列として書式設定することもできます。 次の例では、このメソッドを呼び出して、整数値の配列のバイナリ、8 進数、および 16 進数の表現を表示します。
int[] numbers = { -146, 11043, 2781913 };
Console.WriteLine("{0,8} {1,32} {2,11} {3,10}",
"Value", "Binary", "Octal", "Hex");
foreach (int number in numbers)
{
Console.WriteLine("{0,8} {1,32} {2,11} {3,10}",
number, Convert.ToString(number, 2),
Convert.ToString(number, 8),
Convert.ToString(number, 16));
}
// The example displays the following output:
// Value Binary Octal Hex
// -146 11111111111111111111111101101110 37777777556 ffffff6e
// 11043 10101100100011 25443 2b23
// 2781913 1010100111001011011001 12471331 2a72d9
let numbers = [| -146; 11043; 2781913 |]
printfn $"""{"Value",8} {"Binary",32} {"Octal",11} {"Hex",10}"""
for number in numbers do
printfn $"{number,8} {Convert.ToString(number, 2),32} {Convert.ToString(number, 8),11} {Convert.ToString(number, 16),10}"
// The example displays the following output:
// Value Binary Octal Hex
// -146 11111111111111111111111101101110 37777777556 ffffff6e
// 11043 10101100100011 25443 2b23
// 2781913 1010100111001011011001 12471331 2a72d9
Dim numbers() As Integer = { -146, 11043, 2781913 }
Console.WriteLine("{0,8} {1,32} {2,11} {3,10}", _
"Value", "Binary", "Octal", "Hex")
For Each number As Integer In numbers
Console.WriteLine("{0,8} {1,32} {2,11} {3,10}", _
number, Convert.ToString(number, 2), _
Convert.ToString(number, 8), _
Convert.ToString(number, 16))
Next
' The example displays the following output:
' Value Binary Octal Hex
' -146 11111111111111111111111101101110 37777777556 ffffff6e
' 11043 10101100100011 25443 2b23
' 2781913 1010100111001011011001 12471331 2a72d9
10 進数以外の 32 ビット整数値を操作する
個々の整数を 10 進値として操作するだけでなく、整数値を使用してビットごとの演算を実行したり、整数値のバイナリ表現または 16 進数表現を操作したりできます。 Int32 値は 31 ビットで表され、30 秒ビットは符号ビットとして使用されます。 正の値は、符号と大きさの表現を使用して表されます。 負の値は、2 の補数表現にあります。 これは、 Int32 値に対してビットごとの操作を実行する場合や、個々のビットを操作する場合に注意することが重要です。 2 つの 10 進数以外の値に対して数値、ブール値、または比較演算を実行するには、両方の値が同じ表現を使用する必要があります。
フィールド
| 名前 | 説明 |
|---|---|
| MaxValue |
Int32の可能な最大値を表します。 このフィールドは定数です。 |
| MinValue |
Int32の可能な限り小さい値を表します。 このフィールドは定数です。 |
メソッド
| 名前 | 説明 |
|---|---|
| Abs(Int32) |
値の絶対を計算します。 |
| BigMul(Int32, Int32) |
2 つの 32 ビット数値の完全な積を生成します。 |
| Clamp(Int32, Int32, Int32) |
値を包括的な最小値と最大値にクランプします。 |
| CompareTo(Int32) |
このインスタンスを指定した 32 ビット符号付き整数と比較し、その相対値を示す値を返します。 |
| CompareTo(Object) |
このインスタンスを指定したオブジェクトと比較し、その相対値を示す値を返します。 |
| CopySign(Int32, Int32) |
値の符号を別の値の符号にコピーします。 |
| CreateChecked<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値に対してオーバーフロー例外をスローします。 |
| CreateSaturating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を飽和させます。 |
| CreateTruncating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値を切り捨てます。 |
| DivRem(Int32, Int32) |
2 つの値の商と剰余を計算します。 |
| Equals(Int32) |
このインスタンスが指定した Int32 値と等しいかどうかを示す値を返します。 |
| Equals(Object) |
このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。 |
| GetHashCode() |
このインスタンスのハッシュ コードを返します。 |
| GetTypeCode() | |
| IsEvenInteger(Int32) |
値が偶数の整数を表すかどうかを判断します。 |
| IsNegative(Int32) |
値が負の値かどうかを判断します。 |
| IsOddInteger(Int32) |
値が奇数の整数を表すかどうかを判断します。 |
| IsPositive(Int32) |
値が正かどうかを判断します。 |
| IsPow2(Int32) |
値が 2 の累乗であるかどうかを判断します。 |
| LeadingZeroCount(Int32) |
値の先頭のゼロの数を計算します。 |
| Log10(Int32) |
32 ビット符号付き整数を表します。 |
| Log2(Int32) |
値の log2 を計算します。 |
| Max(Int32, Int32) |
2 つの値を、より大きいコンピューティングと比較します。 |
| MaxMagnitude(Int32, Int32) |
2 つの値を、より大きいコンピューティングと比較します。 |
| Min(Int32, Int32) |
2 つの値を比較して計算します。計算の方が少なくなります。 |
| MinMagnitude(Int32, Int32) |
2 つの値を比較して計算します。計算の方が少なくなります。 |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 |
| Parse(String, IFormatProvider) |
指定したカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。 |
| Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。 |
| Parse(String, NumberStyles) |
指定したスタイルの数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。 |
| Parse(String) |
数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。 |
| PopCount(Int32) |
値に設定されているビット数を計算します。 |
| RotateLeft(Int32, Int32) |
指定した量の値を左に回転します。 |
| RotateRight(Int32, Int32) |
指定した量の値を右に回転します。 |
| Sign(Int32) |
値の符号を計算します。 |
| ToString() |
このインスタンスの数値を等価の文字列形式に変換します。 |
| ToString(IFormatProvider) |
指定したカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。 |
| ToString(String, IFormatProvider) |
指定した書式とカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。 |
| ToString(String) |
指定した形式を使用して、このインスタンスの数値を等価の文字列形式に変換します。 |
| TrailingZeroCount(Int32) |
値の末尾のゼロの数を計算します。 |
| TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在のインスタンスの値を UTF-8 として指定されたバイトスパンに書式設定しようとします。 |
| TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在の整数インスタンスの値を、指定された文字数のスパンに書式設定しようとします。 |
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int32) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Byte>, Int32) |
数値の文字列形式を含む UTF-8 文字スパンを、等価の 32 ビット符号付き整数に変換しようとします。 |
| TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int32) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, Int32) |
文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, Int32) |
カルチャ固有の形式の数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
| TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int32) |
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
| TryParse(String, IFormatProvider, Int32) |
文字列を値に解析しようとします。 |
| TryParse(String, Int32) |
数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
| TryParse(String, NumberStyles, IFormatProvider, Int32) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッド セーフを保証するためにロックによって保護する必要があります。