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.