在十进制、十六进制、二进制、八进制之间实时互转
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.
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 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 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 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.
| Decimal | Binary | Octal | Hexadecimal |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 42 | 101010 | 52 | 2A |
| 100 | 1100100 | 144 | 64 |
| 255 | 11111111 | 377 | FF |
| 1024 | 10000000000 | 2000 | 400 |
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 (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().
| Base | Literal Example | String → Number | Number → String |
|---|---|---|---|
| Binary | 0b1010 → 10 | parseInt('1010', 2) | (10).toString(2) → '1010' |
| Octal | 0o755 → 493 | parseInt('755', 8) | (493).toString(8) → '755' |
| Hexadecimal | 0xFF → 255 | parseInt('FF', 16) | (255).toString(16) → 'ff' |
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.
| Base | Literal Example | String → Number | Number → String |
|---|---|---|---|
| Binary | 0b1010 → 10 | int('1010', 2) | bin(10) → '0b1010' |
| Octal | 0o755 → 493 | int('755', 8) | oct(493) → '0o755' |
| Hexadecimal | 0xFF → 255 | int('FF', 16) | hex(255) → '0xff' |
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.
| Base | Literal Example | String → Number | Number → String |
|---|---|---|---|
| Binary | 0b1010 → 10 | Integer.parseInt("1010", 2) | Integer.toBinaryString(10) |
| Octal | 0755 → 493 | Integer.parseInt("755", 8) | Integer.toOctalString(493) |
| Hexadecimal | 0xFF → 255 | Integer.parseInt("FF", 16) | Integer.toHexString(255) |
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.
| Base | Literal Example | printf Format | C++ Stream Manipulator |
|---|---|---|---|
| Binary | 0b1010 → 10 | No standard specifier | std::bitset |
| Octal | 0755 → 493 | printf("%o", 493) | std::cout << std::oct << 493 |
| Hexadecimal | 0xFF → 255 | printf("%x", 255) | std::cout << std::hex << 255 |
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.
| Base | Literal Example | String → Number | Number → String |
|---|---|---|---|
| Binary | 0b1010 → 10 | strconv.ParseInt("1010", 2, 64) | strconv.FormatInt(10, 2) |
| Octal | 0o755 → 493 | strconv.ParseInt("755", 8, 64) | strconv.FormatInt(493, 8) |
| Hexadecimal | 0xFF → 255 | strconv.ParseInt("FF", 16, 64) | strconv.FormatInt(255, 16) |
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.
| Base | Literal Example | String → Number | Format Output |
|---|---|---|---|
| Binary | 0b1010 → 10 | i32::from_str_radix("1010", 2) | format!("{:b}", 10) |
| Octal | 0o755 → 493 | i32::from_str_radix("755", 8) | format!("{:o}", 493) |
| Hexadecimal | 0xFF → 255 | i32::from_str_radix("FF", 16) | format!("{:x}", 255) |
#3B82F6). When dynamically computing colors in JavaScript, you need to convert between hexadecimal and decimal RGB values.755 or rwxr-xr-x) are essentially octal representations of 3-bit binary values.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.