C# Convert ToString(Int32, Int32)

Description

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

Syntax

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


public static string ToString(
  int value,
  int toBase
)

Parameters

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

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

Returns

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

Example

The following example converts each element in an integer array to its equivalent binary, hexadecimal, decimal, and hexadecimal string representations.


/*from  ww  w.ja  va2  s. c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    int[] bases = { 2, 8, 10, 16};
    int[] numbers = { Int32.MinValue, -123123123, -123123, -18, 12, 
                                 123123, Int32.MaxValue };
    
    foreach (int baseValue in bases)
    {
       Console.WriteLine("Base {0} conversion:", baseValue);
       foreach (int number in numbers)
       {
          Console.WriteLine("   {0,-15}  -->  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