🔢 进制转换

在十进制、十六进制、二进制、八进制之间实时互转

BinaryBase 2
0
OctalBase 8
0
DecimalBase 10
0
HexadecimalBase 16
0
💡 快速参考

What is Number Base Conversion?

Number Base Conversion is the process of converting a numerical value from one base (radix) representation to another. A number base defines the set of digit symbols used in a counting system. In daily life we use decimal (0-9), but computers operate on binary (0-1), and programmers regularly work with hexadecimal (0-9, A-F) and octal (0-7). A number base converter helps developers quickly switch between these representations without manual calculation.

Four Common Number Bases Explained

Binary (Base 2)

Binary is the most fundamental computer language, using only 0 and 1. All low-level hardware — CPUs, memory, registers — operates on binary. Each binary digit (bit) represents a switch state: on (1) or off (0). In programming, bitwise operations (AND, OR, NOT, XOR, shift) directly manipulate binary bits and form the foundation of high-performance computing and embedded development. For example, the decimal number 42 is 101010 in binary.

Octal (Base 8)

Octal uses digits 0 through 7. In early computer systems, octal was widely used to represent binary data because every 3 binary digits map exactly to 1 octal digit. Today, octal is primarily used for Unix/Linux file permissions (e.g., chmod 755), numeric literals in certain programming languages (e.g., JavaScript's 0o10 equals 8 in decimal), and register configurations in embedded systems.

Decimal (Base 10)

Decimal is the numbering system most familiar to humans, using ten digit symbols 0 through 9. It is our "native" base — all everyday calculations, currencies, and measurements use decimal. In programming, decimal is the default numeric representation and our preferred base for reading and entering numbers. When we need to view or input values in other bases, we often convert them to decimal first to understand their "real value."

Hexadecimal (Base 16)

Hexadecimal uses 0-9 and A-F (or a-f), totaling 16 symbols. It is the most commonly used non-decimal representation in programming. Every 4 binary digits correspond exactly to 1 hexadecimal digit, making hex a compact way to represent binary data. Typical use cases include: color codes (e.g., #FF5733 for RGB colors), memory addresses (e.g., 0x7FFF), Unicode code points (e.g., U+4E2D), MAC addresses (e.g., 00:1A:2B:3C:4D:5E), and various data encoding and debugging scenarios.

Base Conversion Reference Table

DecimalBinaryOctalHexadecimal
0000
1111
81000108
10101012A
15111117F
16100002010
42101010522A
100110010014464
25511111111377FF
1024100000000002000400

Number Base Literals and Conversions in Programming Languages

Different programming languages have their own prefixes, suffixes, and built-in functions for representing binary, octal, and hexadecimal numbers. Below is a comprehensive reference of number base syntax and conversion methods in major programming languages.

JavaScript / TypeScript

JavaScript (ES6+) uses the 0b prefix for binary, 0o prefix for octal, and 0x prefix for hexadecimal. Convert between strings and numbers using parseInt() and toString().

BaseLiteral ExampleString → NumberNumber → String
Binary0b1010 → 10parseInt('1010', 2)(10).toString(2)'1010'
Octal0o755 → 493parseInt('755', 8)(493).toString(8)'755'
Hexadecimal0xFF → 255parseInt('FF', 16)(255).toString(16)'ff'

Python

Python uses 0b for binary, 0o for octal, and 0x for hexadecimal. Built-in functions bin(), oct(), and hex() convert integers to base-prefixed strings, while int() parses strings with a specified radix.

BaseLiteral ExampleString → NumberNumber → String
Binary0b1010 → 10int('1010', 2)bin(10)'0b1010'
Octal0o755 → 493int('755', 8)oct(493)'0o755'
Hexadecimal0xFF → 255int('FF', 16)hex(255)'0xff'

Java

In Java, binary literals use the 0b prefix (Java 7+), octal uses a leading 0, and hexadecimal uses 0x. Use Integer.parseInt() for parsing and Integer.toBinaryString() / toOctalString() / toHexString() for formatting.

BaseLiteral ExampleString → NumberNumber → String
Binary0b1010 → 10Integer.parseInt("1010", 2)Integer.toBinaryString(10)
Octal0755 → 493Integer.parseInt("755", 8)Integer.toOctalString(493)
Hexadecimal0xFF → 255Integer.parseInt("FF", 16)Integer.toHexString(255)

C / C++

In C and C++, binary literals use 0b prefix (C++14 / GCC extension), octal uses a leading 0 (e.g., 0755), and hexadecimal uses 0x. Use printf with %x / %o format specifiers or C++ stream manipulators std::hex / std::oct for output.

BaseLiteral Exampleprintf FormatC++ Stream Manipulator
Binary0b1010 → 10No standard specifierstd::bitset
Octal0755 → 493printf("%o", 493)std::cout << std::oct << 493
Hexadecimal0xFF → 255printf("%x", 255)std::cout << std::hex << 255

Go

Go uses 0b prefix for binary (Go 1.13+), 0o prefix for octal (Go 1.13+), and 0x prefix for hexadecimal. Use strconv.ParseInt() for parsing and strconv.FormatInt() for formatting output.

BaseLiteral ExampleString → NumberNumber → String
Binary0b1010 → 10strconv.ParseInt("1010", 2, 64)strconv.FormatInt(10, 2)
Octal0o755 → 493strconv.ParseInt("755", 8, 64)strconv.FormatInt(493, 8)
Hexadecimal0xFF → 255strconv.ParseInt("FF", 16, 64)strconv.FormatInt(255, 16)

Rust

Rust uses 0b prefix for binary, 0o prefix for octal, and 0x prefix for hexadecimal. Parse strings via from_str_radix() and format output using the format! macro with {:b}, {:o}, {:x} specifiers.

BaseLiteral ExampleString → NumberFormat Output
Binary0b1010 → 10i32::from_str_radix("1010", 2)format!("{:b}", 10)
Octal0o755 → 493i32::from_str_radix("755", 8)format!("{:o}", 493)
Hexadecimal0xFF → 255i32::from_str_radix("FF", 16)format!("{:x}", 255)

Practical Use Cases for Number Base Conversion

Why Use an Online Number Base Converter?

While manually computing base conversions is a fundamental computer science skill, doing it by hand during actual development work is inefficient and error-prone. This is especially true when dealing with large numbers (such as 64-bit integers) or negative numbers, where manual calculation can easily lead to mistakes. Our online number base converter supports real-time conversion between all four common bases. Enter a number in any base and instantly see the corresponding results in the other three bases. All calculations are performed locally in your browser using BigInt, supporting very large integers and negative numbers — no installation required and no data is ever uploaded to any server. Whether you are debugging code, writing documentation, or learning computer science fundamentals, this is your handiest number base conversion companion.