Convert.ToString Method (Int16, Int32)

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

Converts the value of a 16-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 Short, _
    toBase As Integer _
) As String
[SecuritySafeCriticalAttribute]
public static string ToString(
    short 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 16-bit integers to String s with the ToString method in the radixes supported by the method.

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

Module Example

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

      Dim values As Short() = { _
          Short.MinValue, _
          -100, _
          999, _
          Short.MaxValue}
      Dim radices As Integer() = {2, 8, 10, 16}

      ' Iterate through the values array.
      Dim value As Short
      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,8}  {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( Short, Integer ) " & _
          "generates " & vbCrLf & "the following output. It " & _
          "converts several Short 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( Short, Integer ) generates
' the following output. It converts several Short values to
' strings using the radixes supported by the method.
' 
'    Value  Radix  String
'    -----  -----  ------
'   -32768    2    1000000000000000
'   -32768    8    100000
'   -32768   10    -32768
'   -32768   16    8000
'     -100    2    1111111110011100
'     -100    8    177634
'     -100   10    -100
'     -100   16    ff9c
'      999    2    1111100111
'      999    8    1747
'      999   10    999
'      999   16    3e7
'    32767    2    111111111111111
'    32767    8    77777
'    32767   10    32767
'    32767   16    7fff
// Example of the Convert.ToString( short, int ) method.
using System;

class Example
{
   static void RunToStringDemo(System.Windows.Controls.TextBlock outputBlock)
   {
      short[] values = {
            short.MinValue, 
            -100, 
            999, 
            short.MaxValue };
      int[] radices = { 2, 8, 10, 16 };

      // Iterate through the values array.
      foreach (short 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,8}  {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( short, int ) " +
          "generates \nthe following output. It converts several " +
          "short 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( short, int ) generates
the following output. It converts several short values to
strings using the radixes supported by the method.

   Value  Radix  String
   -----  -----  ------
  -32768    2    1000000000000000
  -32768    8    100000
  -32768   10    -32768
  -32768   16    8000
    -100    2    1111111110011100
    -100    8    177634
    -100   10    -100
    -100   16    ff9c
     999    2    1111100111
     999    8    1747
     999   10    999
     999   16    3e7
   32767    2    111111111111111
   32767    8    77777
   32767   10    32767
   32767   16    7fff
*/

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.