
Number base converter
Convert a whole number between binary, octal, decimal and hexadecimal bases.
Representations are shown without base prefixes; the minus sign is preserved except for zero.
Method and assumptions
Method version 1.0.0 · Updated
Convert a number
Formula: Value = sum of each digit × base^position, with an optional sign.
Shows the same whole-number value in binary, octal, decimal and hexadecimal. Prefixes such as 0b, 0o and 0x are not required; negative numbers are supported.
- value
- Number to convert
- base
- Input number base
Signed whole numbers from 1 to 256 digits in bases 2, 8, 10 or 16. Digits must belong to the selected base.
Calculate with code
These short examples use the same inputs and core calculation as this tool. Adapt validation and formatting to your application.
JavaScript
const value = 45n;
console.log(value.toString(2), value.toString(8), value.toString(16).toUpperCase()); // 101101 55 2DTypeScript
const value: bigint = 45n;
console.log(value.toString(2), value.toString(8), value.toString(16).toUpperCase()); // 101101 55 2DPython
value = 45
print(format(value, 'b'), format(value, 'o'), format(value, 'X')) # 101101 55 2DJava
long value = 45;
System.out.printf("%s %s %X%n", Long.toBinaryString(value), Long.toOctalString(value), value); // 101101 55 2DC#
int value = 45;
Console.WriteLine($"{Convert.ToString(value, 2)} {Convert.ToString(value, 8)} {value:X}"); // 101101 55 2DMore languages
C
#include <stdio.h>
int main(void) { int value = 45; printf("%o %X\n", value, value); } // 55 2D; use repeated division for binaryC++
#include <bitset>
#include <iostream>
int main() { int value = 45; std::cout << std::bitset<8>(value) << " " << std::oct << value << " " << std::hex << value; } // 00101101 55 2dPHP
$value = 45;
echo decbin($value), ' ', decoct($value), ' ', strtoupper(dechex($value)); // 101101 55 2DPowerShell
$value = 45
'{0} {1} {2:X}' -f [Convert]::ToString($value, 2), [Convert]::ToString($value, 8), $value # 101101 55 2D