How To Work With Different Numbering Systems In C#
[C#]
When it comes to numbers, we overwhelmingly work with the decimal number system, which uses a base of 10.
When say the following:
int number = 1000;
We (human beings) and the C# compiler and runtime understand this to mean 1,000
in base 10
.
The C# compiler and runtime, however, support multiple numbering systems.
If we wanted to use a base of hexadecimal, or hex, which is to say base 16
, we prefix the number with Ox
. This is called a number literal.
int number = 0x1000;
If we write this number to the console,
Console.WriteLine(number);
the following will be printed
4096
We can also get a hex representation of a number by using the format specifier x
.
int number = 1000;
var hexRepresentation = 1000.ToString("x");
Console.WriteLine(hexRepresentation);
This will print the following:
3e8
C# also supports the binary numbering system, base 2.
To use a binary number literal, we prefix the number with 0b
int number = 0b1000;
Console.WriteLine(number);
This will print the following
8
If we already have a number and want to display its decimal equivalent, we can use the format specifier b
var number = 8;
Console.WriteLine(number.ToString("b"));
We can also indicate we are using an octal numbering system, base 8.
This requries a bit more work, as there are no number literals for octal.
To indicate a number is octal, you first convert it to a string and then use an overload of the Convert.ToInt32 that takes a base as a parameter.
int number = 1000;
var octalRepresentation = Convert.ToInt32(number.ToString(), 8);
Console.WriteLine(octalRepresentation);
This will print the following:
512
At this point, you might ask what other numbering systems the runtime supports if Convert.ToInt32
allows you to specify the base. The only supported bases are 2
, 8
, 10
and 16
.
Given the number system operations work with integers (and not other numeric types like float and decimal), this logic will also work with:
Happy hacking!