次の方法で共有


Convert.ToString メソッド (Int32, Int32)

32 ビット符号付き整数の値を、指定した基数で等価の String 形式に変換します。

Overloads Public Shared Function ToString( _
   ByVal value As Integer, _   ByVal toBase As Integer _) As String
[C#]
public static string ToString(intvalue,inttoBase);
[C++]
public: static String* ToString(intvalue,inttoBase);
[JScript]
public static function ToString(
   value : int,toBase : int) : String;

パラメータ

  • value
    32 ビット符号付き整数。
  • toBase
    戻り値の基数。これは 2、8、10、または 16 である必要があります。

戻り値

基数 toBase での value の String 形式。

例外

例外の種類 条件
ArgumentException toBase が 2、8、10、または 16 ではありません。

使用例

[Visual Basic, C#, C++] ToString メソッドを使用し、このメソッドがサポートする基数に従って、複数の Int32 (32 ビット整数) を String に変換するコード例を次に示します。

 
' Example of the Convert.ToString( Integer, Integer ) method.
Imports System
Imports Microsoft.VisualBasic

Module ConvertRadixIntDemo
    
    Sub RunToStringDemo( )

        Dim values as Integer( ) = { _
            Integer.MinValue, _
            -100, _
            0, _
            99999, _
            Integer.MaxValue }
        Dim radices as Integer( ) = { 2, 8, 10, 16 }

        ' Iterate through the values array.
        Dim value as Integer
        For Each value in values

            ' Iterate through the radices.
            Dim radix as Integer
            For Each radix in radices

                ' Convert a value with a radix.
                Dim valueString As String = _
                    Convert.ToString( value, radix )

                Console.WriteLine( "{0,13}  {1,3}    {2}", _
                    value, radix, valueString )
            Next radix
        Next value
    End Sub 

    Sub Main( )

        Console.WriteLine( _
            "This example of Convert.ToString( Integer, Integer " & _ 
            ") generates " & vbCrLf & "the following output. It " & _
            "converts several Integer values to " & vbCrLf & _
            "strings using the radixes supported by the method." ) 
        Console.WriteLine( vbCrLf & _
            "     Value     Radix    String" & vbCrLf & _
            "     -----     -----    ------" )

        RunToStringDemo( )

    End Sub 
End Module 

' This example of Convert.ToString( Integer, Integer ) generates
' the following output. It converts several Integer values to
' strings using the radixes supported by the method.
' 
'      Value     Radix    String
'      -----     -----    ------
'   -2147483648    2    10000000000000000000000000000000
'   -2147483648    8    20000000000
'   -2147483648   10    -2147483648
'   -2147483648   16    80000000
'          -100    2    11111111111111111111111110011100
'          -100    8    37777777634
'          -100   10    -100
'          -100   16    ffffff9c
'             0    2    0
'             0    8    0
'             0   10    0
'             0   16    0
'         99999    2    11000011010011111
'         99999    8    303237
'         99999   10    99999
'         99999   16    1869f
'    2147483647    2    1111111111111111111111111111111
'    2147483647    8    17777777777
'    2147483647   10    2147483647
'    2147483647   16    7fffffff

[C#] 
// Example of the Convert.ToString( int, int ) method.
using System;

class ConvertRadixIntDemo
{
    static void RunToStringDemo( )
    {
        int[ ] values = {
            int.MinValue, 
            -100, 
            0, 
            99999, 
            int.MaxValue };
        int[ ]  radices = { 2, 8, 10, 16 };

        // Iterate through the values array.
        foreach( int value in values )
        {
            // Iterate through the radices.
            foreach( int radix in radices )
            {
                // Convert a value with a radix.
                string valueString = 
                    Convert.ToString( value, radix );

                Console.WriteLine( "{0,13}  {1,3}    {2}", 
                    value, radix, valueString );
            }
        }
    } 

    static void Main( )
    {
        Console.WriteLine(
            "This example of Convert.ToString( int, int ) " +
            "generates \nthe following output. It converts several " +
            "int values to \nstrings using the radixes supported " +
            "by the method." );
        Console.WriteLine( 
            "\n     Value     Radix    String" +
            "\n     -----     -----    ------" );

        RunToStringDemo( );
    } 
} 

/*
This example of Convert.ToString( int, int ) generates
the following output. It converts several int values to
strings using the radixes supported by the method.

     Value     Radix    String
     -----     -----    ------
  -2147483648    2    10000000000000000000000000000000
  -2147483648    8    20000000000
  -2147483648   10    -2147483648
  -2147483648   16    80000000
         -100    2    11111111111111111111111110011100
         -100    8    37777777634
         -100   10    -100
         -100   16    ffffff9c
            0    2    0
            0    8    0
            0   10    0
            0   16    0
        99999    2    11000011010011111
        99999    8    303237
        99999   10    99999
        99999   16    1869f
   2147483647    2    1111111111111111111111111111111
   2147483647    8    17777777777
   2147483647   10    2147483647
   2147483647   16    7fffffff
*/

[C++] 
// Example of the Convert::ToString( int, int ) method.
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void RunToStringDemo( )
{
    int values __gc [ ] = {
        Int32::MinValue, 
        -100, 
        0, 
        99999, 
        Int32::MaxValue };
    int  radices[ 4 ] = { 2, 8, 10, 16 };

    // Implement foreach( int value in values ).
    IEnumerator*    myEnum = values->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        int value = Convert::ToInt32( myEnum->Current );

        // Iterate through the radices.
        for( int i = 0; i < 4; i++ )
        {
            // Convert a value with a radix.
            int radix = radices[ i ];
            String* valueString = 
                Convert::ToString( value, radix );

            Console::WriteLine( S"{0,12} {1,3}   {2}", 
                __box( value ), __box( radix ), valueString );
        }
    }
} 

void main()
{
    Console::WriteLine(
        S"This example of Convert::ToString( int, int ) " 
        S"generates \nthe following output. It converts several " 
        S"int values to \nstrings using the radixes supported " 
        S"by the method." );
    Console::WriteLine( 
        S"\n    Value    Radix   String" 
        S"\n    -----    -----   ------" );

    RunToStringDemo( );
} 

/*
This example of Convert::ToString( int, int ) generates
the following output. It converts several int values to
strings using the radixes supported by the method.

    Value    Radix   String
    -----    -----   ------
 -2147483648   2   10000000000000000000000000000000
 -2147483648   8   20000000000
 -2147483648  10   -2147483648
 -2147483648  16   80000000
        -100   2   11111111111111111111111110011100
        -100   8   37777777634
        -100  10   -100
        -100  16   ffffff9c
           0   2   0
           0   8   0
           0  10   0
           0  16   0
       99999   2   11000011010011111
       99999   8   303237
       99999  10   99999
       99999  16   1869f
  2147483647   2   1111111111111111111111111111111
  2147483647   8   17777777777
  2147483647  10   2147483647
  2147483647  16   7fffffff
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Convert クラス | Convert メンバ | System 名前空間 | Convert.ToString オーバーロードの一覧