Partager via


Convert.ToString Method (Int32, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the value of a 32-bit signed integer to its equivalent String representation in a specified base.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function ToString ( _
    value As Integer, _
    toBase As Integer _
) As String
[SecuritySafeCriticalAttribute]
public static string ToString(
    int value,
    int toBase
)

Parameters

  • toBase
    Type: System.Int32
    The base of the return value, which must be 2, 8, 10, or 16.

Return Value

Type: System.String
The String representation of value in base toBase.

Exceptions

Exception Condition
ArgumentException

toBase is not 2, 8, 10, or 16.

Remarks

If value is negative and toBase is a non-decimal value, the returned string uses two's complement representation.

Examples

The following code example converts several 32-bit integers to String s with the ToString method in the radixes supported by the method.

' Example of the Convert.ToString( Integer, Integer ) method.

Module Example

   Sub RunToStringDemo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      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)

            outputBlock.Text &= String.Format("{0,13}  {1,3}    {2}", _
                value, radix, valueString) & vbCrLf
         Next radix
      Next value
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format( _
          "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.") & vbCrLf
      outputBlock.Text &= String.Format(vbCrLf & _
          "     Value     Radix    String" & vbCrLf & _
          "     -----     -----    ------") & vbCrLf

      RunToStringDemo(outputBlock)

   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
// Example of the Convert.ToString( int, int ) method.
using System;

class Example
{
   static void RunToStringDemo(System.Windows.Controls.TextBlock outputBlock)
   {
      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);

            outputBlock.Text += String.Format("{0,13}  {1,3}    {2}",
                value, radix, valueString) + "\n";
         }
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format(
          "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.") + "\n";
      outputBlock.Text += String.Format(
          "\n     Value     Radix    String" +
          "\n     -----     -----    ------") + "\n";

      RunToStringDemo(outputBlock);
   }
}

/*
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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.