Convert.ToString Method (Int16, Int32)

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)

public static string ToString (
	short value,
	int toBase
)
public static String ToString (
	short value, 
	int toBase
)
public static function ToString (
	value : short, 
	toBase : int
) : String
Not applicable.

Parameters

value

A 16-bit signed integer.

toBase

The base of the return value, which must be 2, 8, 10, or 16.

Return Value

The String representation of value in base toBase.

Exception typeCondition

ArgumentException

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

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

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, int ) method.
using System;

class ConvertRadixShortDemo
{
    static void RunToStringDemo( )
    {
        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 );

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

    static void Main( )
    {
        Console.WriteLine(
            "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." );
        Console.WriteLine( 
            "\n   Value  Radix  String" +
            "\n   -----  -----  ------" );

        RunToStringDemo( );
    } 
} 

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

// Example of the Convert.ToString( short, int ) method.
import System.* ;

class ConvertRadixShortDemo
{   
    static void RunToStringDemo() 
    {
        short values[] =  { -32768, -100, 999, 32767 };
        int radices[] =  { 2, 8, 10, 16 };
        // Iterate through the values array.
        for (int iCtr = 0; iCtr < values.length; iCtr++) {
            short value = values[iCtr];
            // Iterate through the radices.
            for (int iCtr1 = 0 ; iCtr1 < radices.length ; iCtr1++) {
                int radix = radices[iCtr1];
                  // Convert a value with a radix.
                String valueString = Convert.ToString(value, radix);
                Console.WriteLine("{0,8}  {1,3}    {2}",
                    System.Convert.ToString(value),
                    System.Convert.ToString(radix), valueString);
            }
        }
    } //RunToStringDemo   
   
    public static void main(String[] args)
    {
        Console.WriteLine(("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."));
        Console.WriteLine(("\n   Value  Radix  String" 
            + "\n   -----  -----  ------"));
        RunToStringDemo();
    } //main
} //ConvertRadixShortDemo

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show: