Convert int to its equivalent string representation in a specified base in CSharp

Description

The following code shows how to convert int to its equivalent string representation in a specified base.

Example


using System;/*  w  w w .ja  va  2  s .  c o m*/
public class MainClass
{
    public static void Main()
    {
        int[] bases = { 2, 8, 10, 16 };
        int[] numbers = { Int32.MinValue, -123, -12345, -18, 12, 19, 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 »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var