devxlogo

Convert a decimal value to binary, octal, or hexadecimal

Convert a decimal value to binary, octal, or hexadecimal

The ToString method of the Convert class lets you easily and quickly convert a decimal value into a string representation of that number to binary, octal, or hexadecimal base:

' convert to binaryConsole.WriteLine(Convert.ToString(11, 2))    ' => 1011' convert to octalConsole.WriteLine(Convert.ToString(123, 8))    ' => 173' convert to hexadecimalConsole.WriteLine(Convert.ToString(254, 16))    ' => fe

The ToString method is overloaded to take a Byte, a Short, an Integer, or a Long value in the first argument; the second argument can only be 2, 8, 10, or 16, else an exception will be thrown.

devx-admin

Share the Post: