WikiPlus

FAQ: Number Base Conversion Questions Answered

Number base conversion generates a consistent set of questions from students, developers, and curious learners. What is the difference between binary and hex? How do I convert in my programming language? Why does hex start with 0x? What is BigInt and why does it matter? This FAQ answers the most common questions clearly and practically, with examples and pointers to the tools that make these conversions easy.

Fundamentals: How the Different Number Bases Work

Q: What is a number base (or radix)? A number base is the count of unique digits in a positional numeral system. Base 10 (decimal) has 10 digits (0-9). Base 2 (binary) has 2 digits (0, 1). Base 8 (octal) has 8 digits (0-7). Base 16 (hexadecimal) has 16 digits (0-9 and A-F). The position of each digit in a number determines its weight: digit × base^position. Q: Why do computers use binary? Because digital electronics are built from transistors, which naturally have two states: on (1) and off (0). It is extremely reliable to distinguish between two states (high voltage vs. low voltage). Ten states (as decimal would require) would be much harder to distinguish reliably in electronic circuits. Binary maximizes reliability and simplicity. Q: Why is hexadecimal useful? Hexadecimal is a compact shorthand for binary. Each hex digit represents exactly 4 binary digits (bits). This means a byte (8 bits) is always 2 hex digits (e.g., 11111111 = FF). Hex is much easier to read than long binary strings while still mapping directly to binary without arithmetic. Q: Is octal still relevant? Yes, in Unix/Linux file permissions (chmod 755) and some legacy contexts. Octal is convenient because each digit represents exactly 3 binary bits, and Unix permission sets are naturally 3 bits each (read/write/execute). For modern development beyond Unix administration, octal is rarely used.

Conversion Questions: How to Do the Math

Q: What is the easiest way to convert binary to decimal? Sum the powers of 2 for each position where there is a 1. From right to left, positions have values 1, 2, 4, 8, 16, 32, 64, 128, .... Example: 10110 = 16+4+2 = 22. Q: How do I convert decimal to binary? Repeatedly divide by 2, collecting remainders, and read them from bottom to top. Or: repeatedly subtract the largest fitting power of 2 and mark that bit as 1. Both methods produce the same result. Q: What is the fastest conversion between binary and hex? Binary to hex: group bits into groups of 4 from the right (pad with leading zeros if needed) and convert each group with the nibble table (0000=0 through 1111=F). Hex to binary: replace each hex digit with its 4-bit binary equivalent. No arithmetic — pure table lookup. Q: How do I convert between hex and decimal without a calculator? For small values (2 hex digits = 1 byte): expand by place values. 0xAB = 10×16 + 11 = 171. For larger values, it gets tedious — use our Number Base Converter for instant results. For the reverse (decimal to hex), repeatedly divide by 16, using A-F for remainders 10-15. Q: How do I convert any base to any other base? The universal method: convert the source number to decimal first (expand by place values), then convert decimal to the target base (repeated division by target base). For power-of-2 bases (binary, octal, hex), use bit grouping shortcuts when going between them.

Programming-Specific Questions

Q: How do I write binary, octal, and hex numbers in my programming language? Most modern languages use: 0b for binary (0b1010), 0o for octal (0o755), 0x for hex (0xFF). In Python, Java, JavaScript, C++, Rust, and Go, these prefixes are standard. Exception: in C (before C++14) and older JavaScript (pre-ES6), binary literals were not supported. Q: How do I convert a number to binary/hex string in code? Python: bin(42) → '0b101010', hex(42) → '0x2a', oct(42) → '0o52'. Remove prefix: bin(42)[2:] → '101010'. JavaScript: (42).toString(2) → '101010', (42).toString(16) → '2a', (42).toString(8) → '52'. Java: Integer.toBinaryString(42) → '101010', Integer.toHexString(42) → '2a'. Q: What is BigInt and why does the Number Base Converter use it? JavaScript's standard Number type can only represent integers exactly up to 2^53 (about 9 quadrillion). Larger integers lose precision. BigInt is a JavaScript type for arbitrary-precision integers — there is no overflow. Our Number Base Converter uses BigInt internally so it can correctly convert very large values like 64-bit memory addresses (up to 2^64) without precision errors. Q: How do I detect if a string is in hex, binary, or decimal format in code? Check for known prefixes: if the string starts with '0x' it is hex; '0b' is binary; '0o' is octal. Without prefixes, check the character set: binary contains only 0 and 1; octal only 0-7; hex only 0-9 and A-F; decimal only 0-9. In Python: int('ff', 16) parses hex; int('1010', 2) parses binary.

Common Mistakes and Misconceptions

Q: Does the digit '8' or '9' exist in octal? No. Octal (base 8) only has digits 0 through 7. If you see an '8' or '9' in what is supposed to be an octal number, it is invalid. A common source of confusion: in C, a leading zero marks an integer as octal — so 019 in C is a syntax error because '9' is not a valid octal digit. Q: Why does 0755 in C behave differently than 755? In C/C++ (and older JavaScript), a leading zero without 'b' or 'x' marks the literal as octal. So 0755 = octal 755 = decimal 493. But 755 = decimal 755. For chmod permission values in C system calls, you must use the 0 prefix: chmod(path, 0755), not chmod(path, 755). This is a classic C gotcha that catches many programmers. Q: Is hexadecimal the same as Base64? No. Hexadecimal (base 16) represents each 4-bit nibble as one character; 2 hex characters per byte. Base64 encodes 6 bits per character, using 64-character alphabet, requiring approximately 1.33 characters per byte. Base64 is more compact for encoding binary data as text. They serve different purposes: hex is a number base for arithmetic and bit representation; Base64 is specifically a binary-to-text encoding. Q: Can negative numbers be represented in any base? Yes. A minus sign can prefix any number in any base: -1011 (binary), -FF (hex). In computers, signed integers are typically stored using two's complement representation (no minus sign — the sign is encoded in the most significant bit). Two's complement allows addition and subtraction to work identically for positive and negative numbers. Q: Is there such a thing as base 3, base 5, or other bases? Yes — any integer ≥ 2 can be a number base. Base 3 (ternary) has digits 0, 1, 2. Base 5 (quinary) has 0-4. Base 12 (duodecimal) has 0-9 and A-B. Base 60 (sexagesimal) is used for time (60 seconds/minute) and angles (360 degrees). In computing, however, only binary (2), octal (8), decimal (10), and hexadecimal (16) are practically relevant.

Frequently Asked Questions

Why does counting in binary go 0, 1, 10, 11, 100... instead of 0, 1, 2, 3, 4?
Because binary only has two digits (0 and 1). When you run out of digits, you carry to the next position — exactly like decimal carrying when you reach 9 and go to 10. In binary, 1+1=10 (one-zero in binary, meaning 2 in decimal). The binary sequence 0, 1, 10, 11, 100, 101, 110, 111, 1000... corresponds to decimal 0, 1, 2, 3, 4, 5, 6, 7, 8 — it is the same counting, just with base-2 place values.
How many hexadecimal digits does it take to represent any 32-bit number?
Exactly 8 hex digits. A 32-bit number has 32 bits. Each hex digit represents 4 bits. 32/4 = 8. So any 32-bit value fits in 8 hex digits (e.g., 0xFFFFFFFF is the maximum unsigned 32-bit value, decimal 4,294,967,295). Similarly: 8-bit = 2 hex digits, 16-bit = 4 hex digits, 64-bit = 16 hex digits.
What happens when I add two large binary numbers and the result does not fit in the bit width?
This is called integer overflow. The result wraps around, losing the most significant bit (the carry out). For example, adding 11111111 (255) + 00000001 (1) in an 8-bit unsigned context gives 100000000 (256), but the 9th bit is discarded, leaving 00000000 (0). In signed arithmetic, overflow can cause a positive number to become negative (wrapping from 127 to -128 in signed 8-bit). Overflow is a critical bug to avoid in security-sensitive code, as it can cause memory corruption and vulnerabilities.