C# Convert ToString(Int16, Int32)

Description

Convert ToString(Int16, Int32) converts the value of a 16-bit signed integer to its equivalent string representation in a specified base.

Syntax

Convert.ToString(Int16, Int32) has the following syntax.


public static string ToString(
  short value,
  int toBase
)

Parameters

Convert.ToString(Int16, Int32) has the following parameters.

  • value - The 16-bit signed integer to convert.
  • toBase - The base of the return value, which must be 2, 8, 10, or 16.

Returns

Convert.ToString(Int16, Int32) method returns The string representation of value in base toBase.

Example

The following example converts each element in an array of 16-bit signed integers to its equivalent binary, octal, decimal, and hexadecimal string representations.


//from  w w w .ja  v  a2  s  .co m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    int[] bases = { 2, 8, 10, 16};
    short[] numbers = { Int16.MinValue, -13621, -18, 12, 19142, Int16.MaxValue };
    
    foreach (int baseValue in bases)
    {
       Console.WriteLine("Base {0} conversion:", baseValue);
       foreach (short number in numbers)
       {
          Console.WriteLine("   {0,-8}  -->  0x{1}", 
                            number, Convert.ToString(number, baseValue));
       }
    }

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version