Binary to BCD Converter
Convert binary numbers to BCD (Binary-Coded Decimal) and BCD to binary instantly. Supports arbitrary-length inputs with 8421 encoding.
About
Binary-Coded Decimal (BCD) maps each decimal digit to a fixed-length 4-bit binary nibble using the 8421 weighting scheme. The encoding is not equivalent to standard positional binary. A raw binary value 1111 represents decimal 15, but in BCD the nibble 1111 is invalid because only patterns 0000 - 1001 (decimal 0 - 9) are legal. Confusing the two encodings in hardware registers, PLC communications, or financial arithmetic causes silent data corruption. This tool converts between pure binary and 8421 BCD for arbitrary-length integers, validates every nibble, and flags illegal codes immediately.
BCD remains critical in digital clocks, calculators, SCADA displays, and any system where decimal rounding errors are unacceptable. The 8421 variant is specified in IEEE 754-2008 for decimal floating-point and in IEC 60044 metering standards. Note: this converter handles unsigned integers only. Signed BCD representations (tensβ complement, sign nibble) require additional protocol knowledge beyond this toolβs scope.
Formulas
The 8421 BCD encoding converts a decimal integer N into a sequence of 4-bit nibbles, one per decimal digit.
Binary β BCD conversion:
Step 1. Parse the binary string to obtain decimal value N:
N = nβ1βi=0 bi β 2i
Step 2. Extract each decimal digit dk via repeated division:
dk = N mod 10, N β N10
Step 3. Encode each digit dk β {0, 1, β¦, 9} as its 4-bit binary representation.
BCD β Binary conversion:
Step 1. Split BCD string into 4-bit nibbles (pad with leading zeros if length is not a multiple of 4).
Step 2. Validate each nibble: dk β€ 9. If dk β₯ 10, the nibble 1010 - 1111 is an illegal BCD code.
Step 3. Reconstruct decimal: N = mβ1βk=0 dk β 10k
Step 4. Convert N to binary: N.toString(2).
Where bi = the i-th bit of the binary input, n = total bit count, dk = the k-th decimal digit (least-significant first), and m = number of BCD nibbles.
Reference Data
| Decimal Digit | BCD (8421) | Binary Value of Nibble | Excess-3 BCD | Gray Code |
|---|---|---|---|---|
| 0 | 0000 | 0 | 0011 | 0000 |
| 1 | 0001 | 1 | 0100 | 0001 |
| 2 | 0010 | 2 | 0101 | 0011 |
| 3 | 0011 | 3 | 0110 | 0010 |
| 4 | 0100 | 4 | 0111 | 0110 |
| 5 | 0101 | 5 | 1000 | 0111 |
| 6 | 0110 | 6 | 1001 | 0101 |
| 7 | 0111 | 7 | 1010 | 0100 |
| 8 | 1000 | 8 | 1011 | 1100 |
| 9 | 1001 | 9 | 1100 | 1101 |
| Nibbles 1010 - 1111 are invalid in 8421 BCD | ||||
| 10 | 0001 0000 | 1010 (binary) | 0100 0011 | - |
| 42 | 0100 0010 | 101010 (binary) | 0111 0101 | - |
| 99 | 1001 1001 | 1100011 (binary) | 1100 1100 | - |
| 100 | 0001 0000 0000 | 1100100 (binary) | 0100 0011 0011 | - |
| 255 | 0010 0101 0101 | 11111111 (binary) | 0101 1000 1000 | - |
| 1000 | 0001 0000 0000 0000 | 1111101000 (binary) | 0100 0011 0011 0011 | - |
| 9999 | 1001 1001 1001 1001 | 10011100001111 (binary) | 1100 1100 1100 1100 | - |