Convert String to a Number in base 2, 8 10 and 16 in CSharp
Description
The following code shows how to convert String to a Number in base 2, 8 10 and 16.
Example
using System;/*w w w .ja v a 2s .c om*/
using System.Data;
class Class1{
static void Main(string[] args){
string base2 = "111";
string base8 = "117";
string base10 = "1110";
string base16 = "11F1FF";
Console.WriteLine("Convert.ToInt32(base2, 2) = " +
Convert.ToInt32(base2, 2));
Console.WriteLine("Convert.ToInt32(base8, 8) = " +
Convert.ToInt32(base8, 8));
Console.WriteLine("Convert.ToInt32(base10, 10) = " +
Convert.ToInt32(base10, 10));
Console.WriteLine("Convert.ToInt32(base16, 16) = " +
Convert.ToInt32(base16, 16));
}
}
The code above generates the following result.