What is a decimal to BCD converter?
A decimal to BCD converter takes a normal base-10 number and re-encodes each of its digits as a separate 4-bit binary value. That encoding is called Binary-Coded Decimal — BCD for short. Instead of converting the whole number to base 2 like pure binary would, BCD keeps the decimal digits visible: 254 becomes 0010 0101 0100, where 0010 is the digit 2, 0101 is the digit 5, and 0100 is the digit 4.
This is different from what most people mean by "convert to binary." Decimal 254 in pure binary is 11111110 — eight bits, one value. In BCD it is twelve bits, three nibbles, three digits. BCD is less efficient at packing data, but it has one decisive advantage: every decimal digit stays individually addressable. That makes it the natural encoding for anything that has to display or compute with decimal digits directly — digital clocks, seven-segment displays, financial ledgers, and the real-time clock chips buried inside almost every modern computer.
This tool converts in both directions, shows the per-digit breakdown for every value, compares BCD against pure binary side by side, and supports the two encodings that show up most often in practice — the standard 8421 code and the self-complementing Excess-3 alternative. In BCD → Decimal mode it also flags invalid nibbles — the six patterns 1010 through 1111 that a well-formed BCD stream never contains.
💡 Quick example. 42 in decimal is 0100 0010 in BCD — digit 4 becomes 0100, digit 2 becomes 0010. Packed into bytes, that is one byte: 0x42. Note that 0x42 looks like the decimal number 42 but the hex value it stores is 66 — that is exactly the packed-BCD trick: the digits of the decimal number are stored as hex digits.
The 8421 BCD table — decimal digit to 4-bit code
Every decimal digit has exactly one 4-bit BCD representation in the standard 8421 encoding. The four bit positions carry weights 8, 4, 2 and 1 from left to right, so the digit is the sum of the weights where the corresponding bit is set.
| Decimal digit | BCD 8421 | 8·4·2·1 breakdown | Pure binary |
|---|---|---|---|
| 0 | 0000 | 0+0+0+0 | 0 |
| 1 | 0001 | 0+0+0+1 | 1 |
| 2 | 0010 | 0+0+2+0 | 10 |
| 3 | 0011 | 0+0+2+1 | 11 |
| 4 | 0100 | 0+4+0+0 | 100 |
| 5 | 0101 | 0+4+0+1 | 101 |
| 6 | 0110 | 0+4+2+0 | 110 |
| 7 | 0111 | 0+4+2+1 | 111 |
| 8 | 1000 | 8+0+0+0 | 1000 |
| 9 | 1001 | 8+0+0+1 | 1001 |
Note that for the digits 0 through 9, BCD and pure binary are identical. They only diverge once you have more than one decimal digit: the decimal number 42 in BCD is 0100 0010, but in pure binary it is 101010.
How to convert decimal to BCD
The conversion is genuinely simple — it is a lookup, not a calculation. Three steps:
- Split the decimal number into its individual digits.
254becomes2,5,4. - Replace each digit with its 4-bit 8421 code.
2→0010,5→0101,4→0100. - Join the nibbles in the original digit order.
0010 0101 0100. That is the BCD representation of 254.
Worked example — 2025:
- Digits:
2,0,2,5. - BCD codes:
0010,0000,0010,0101. - Combined:
0010 0000 0010 0101— 16 bits, four nibbles.
Compare with pure binary: the decimal number 2025 is 11111101001 (11 bits). BCD uses five more bits for exactly the same value. That is the price of per-digit readability.
Packed BCD vs unpacked BCD
BCD has two common storage layouts, and it matters which one you mean when you say "the BCD value of 42".
- Unpacked BCD — each decimal digit occupies a full byte. Only the low nibble carries the digit; the high nibble is usually zero (or set to
1111in the extended form used by EBCDIC). Decimal 42 in unpacked BCD is two bytes:0000 0100and0000 0010. - Packed BCD — two decimal digits share one byte. The more-significant digit sits in the high nibble, the less-significant in the low nibble. Decimal 42 in packed BCD is one byte:
0100 0010, written as0x42.
Packed BCD is what you meet in most real systems: COBOL's COMP-3 packed-decimal format, IBM's packed-decimal instructions, the DS1307 real-time clock chip, and the values this tool emits when you choose the Packed hex output format. The rule of thumb is: if the number of decimal digits is odd, packed BCD pads the leading nibble with zero to fill the byte.
💡 Why "0x42" for decimal 42? The packed representation is not a hex value that happens to be 42 — it is the hex digits 4 and 2 stitched into one byte. When you read packed BCD, you read each nibble as a decimal digit, not as a hex value that contributes to a single number. That is the whole point of the encoding: the byte 0x42 visually contains 4 and 2.
Byte boundaries and odd digit counts
Packing works cleanly when a decimal number has an even number of digits. When the digit count is odd, there is one leftover nibble that cannot pair with another, so systems pad it. There are two conventions in practice:
| Decimal | Digits | Packed with leading 0 (this tool) | Packed with trailing 0 pad |
|---|---|---|---|
| 7 | 1 | 0x07 | 0x70 |
| 42 | 2 | 0x42 | 0x42 |
| 254 | 3 | 0x0254 | 0x2540 |
| 2025 | 4 | 0x2025 | 0x2025 |
| 12345 | 5 | 0x012345 | 0x123450 |
This tool uses the leading-zero convention because it preserves the numeric value without ambiguity: 0x07 obviously means decimal 7, while 0x70 means decimal 70. When the "padded 0x0 for byte fill" pill appears on a result, that is what happened. If you need trailing-zero padding — as some embedded protocols do — you can pad manually by appending a 0 to your input first.
Invalid BCD codes — the six that never occur
A 4-bit nibble has 16 possible states, but BCD only uses 10 of them. The six patterns 1010, 1011, 1100, 1101, 1110 and 1111 — the binary values 10 to 15, or hex A to F — are never valid digits in a standard 8421 BCD stream.
| Nibble | Hex | Decimal | Why it is invalid |
|---|---|---|---|
| 1010 | A | 10 | No decimal digit is 10 |
| 1011 | B | 11 | No decimal digit is 11 |
| 1100 | C | 12 | No decimal digit is 12 |
| 1101 | D | 13 | No decimal digit is 13 |
| 1110 | E | 14 | No decimal digit is 14 |
| 1111 | F | 15 | Reserved for padding in extended layouts (EBCDIC, COBOL) |
If you see one of these nibbles in a stream that is supposed to be BCD, something is wrong — a data corruption, a wrong-format assumption, or a stream that is actually pure binary. This tool flags every invalid nibble it encounters in BCD → Decimal mode so you can pinpoint where the encoding breaks.
Excess-3 (XS-3) — the self-complementing alternative
Excess-3, sometimes called XS-3 or Stibitz code, is a variant of BCD in which every digit's code is shifted by 3. The decimal digit 0 is encoded as 0011 (three), 1 as 0100 (four), all the way up to 9 as 1100 (twelve). The rule is: take the 8421 BCD code and add 3.
Why bother? XS-3 is self-complementing. Because the code for each digit is exactly three above its value, the 9's complement of a decimal digit can be obtained by inverting every bit — the same trick as 1's complement in binary. That makes decimal subtraction hardware dramatically simpler, which is why XS-3 appeared in early decimal computers and handheld calculators of the 1970s.
This converter supports XS-3 as an alternative encoding alongside 8421, so you can compare the two nibbles side by side in the per-digit breakdown table. In the step table, the "Bit positions (8·4·2·1)" column always shows which bit positions are set — a fixed geometric view of the nibble — while the "8421" and "Excess-3" columns show the resulting codes so you can compare.
BCD addition and the +6 correction
Arithmetic on BCD is not the same as arithmetic on pure binary — there is a correction step. To add two BCD digits, add them as ordinary binary first, then check the result:
- If the binary sum is between 0 and 9 (
0000–1001), it is a valid BCD digit — no correction needed. - If the binary sum is between 10 and 15 (
1010–1111), it is an invalid BCD code — add 6 (0110) to wrap it back into range and produce a carry-out of 1. - If the binary sum generated a carry out of bit 4, the result is also invalid — apply the same +6 correction.
Worked example — 7 + 5 in BCD:
0111+0101=1100(binary sum = 12).1100is invalid BCD (it is1010–1111), so add0110.1100+0110=1 0010.- Result:
0010with a carry-out of 1 — decimal 12. Correct.
This is why BCD arithmetic units are more complex than binary adders — every digit position needs its own correction comparator and a 4-bit adder for the +6 step. This tool only converts representation; it does not perform BCD arithmetic — but knowing the +6 rule helps when you are debugging a device that is producing unexpected BCD results.
Where BCD is used — real applications
- Digital clocks and watches. A clock display shows hours, minutes and seconds as separate decimal numbers. Storing the time as BCD means each digit drives one display segment directly — no arithmetic conversion needed between the counter and the display.
- Real-time clock chips. The DS1307, DS3231 and most other I²C real-time clock ICs store the date and time in BCD. Reading the seconds register gives you two nibbles: the tens-of-seconds digit and the ones-of-seconds digit. The value
0x42in that register means 42 seconds — not the binary number 66. - Seven-segment displays. Every digit of a seven-segment display is driven by a BCD decoder chip (like the classic 7447 or the CD4056). Feed it a 4-bit BCD nibble and it lights the segments for that digit.
- Financial and accounting systems. Money is decimal — one hundred cents to the dollar, and results must be exact. Pure binary floating-point cannot represent 0.10 exactly, which causes rounding errors that compound over many transactions. BCD (and its modern successor, IEEE 754-2019 decimal floating-point) avoids that entirely.
- COBOL packed decimal (COMP-3). Enterprise mainframe databases store monetary fields in packed decimal — two digits per byte, with the last nibble reserved for the sign. When you access one of these fields from a modern system you must unpack it from BCD.
- Taxi meters, petrol pumps, and industrial instruments. Any device that displays a decimal value to the user tends to use BCD internally so the display path stays trivial.
- Legacy BIOS date storage. The real-mode BIOS date functions used to return the date packed into two bytes as BCD (
0x25for the month 25, for example).
BCD vs pure binary — when each is right
The choice between BCD and pure binary is a trade-off between storage density and decimal precision. Pure binary is about 20% more compact but cannot represent decimal fractions exactly; BCD is less compact but decimal arithmetic on it is exact.
| Decimal | Bits in pure binary | Bits in BCD | Note |
|---|---|---|---|
| 7 | 3 | 4 | Single digit |
| 42 | 6 | 8 | Two digits |
| 137 | 8 | 12 | Three digits |
| 999 | 10 | 12 | Three digits, largest 3-digit value |
| 10000 | 14 | 20 | Five digits |
| 1234567890 | 31 | 40 | Ten digits |
Use BCD when: the digits need to be individually addressable, the value will be displayed as decimal digits, decimal arithmetic must be exact, or you are interfacing with hardware that expects BCD. Use pure binary when: raw numeric magnitude is all that matters, storage is tight, and the value will not be shown as separate decimal digits.
Common mistakes when converting decimal to BCD
- Confusing BCD with pure binary. They are identical for 0–9 but completely different for 10 and above. Decimal 45 in binary is
101101; in BCD it is0100 0101. If your output only has one 1 followed by more bits, you probably converted to binary by mistake. - Using hex A–F in BCD digits. A valid BCD nibble only ever has the decimal values 0–9. Writing the nibble
1010to mean "ten" in a BCD context is wrong — it is an invalid code that a decoder will reject. - Forgetting to pad odd-length packed BCD. If you have three decimal digits — say
254— packed BCD needs an even number of nibbles to fill bytes. The leading nibble is padded with0000:0000 0010 0101 0100, four nibbles / two bytes. See the "Byte boundaries" section above for the two padding conventions. - Reading packed BCD as hex. The byte
0x42in packed BCD means decimal 42, not decimal 66. The two nibbles are decimal digits, not hex-value contributors. - Assuming all 4-bit patterns are BCD. Only 10 of the 16 nibbles are valid. If you convert a random byte to "BCD" you will hit
1010–1111roughly 37% of the time. - Using BCD to store negative numbers without a sign convention. BCD is unsigned. To store negatives you must reserve a nibble for the sign, and there is no single universal convention — different systems use different sign nibbles. COBOL COMP-3 uses
0x0Cfor +,0x0Dfor −, and0x0Ffor unsigned. - Expecting BCD arithmetic to be as fast as binary. Adding two BCD nibbles and getting a correct decimal result requires a correction step — if the binary sum of a nibble exceeds 9, you must add 6 (
0110) to fix it. See the "BCD addition" section above.
Privacy and data storage
Everything runs in your browser. The conversion, the per-digit breakdown, the BCD-vs-binary comparison, the batch processing and the reference table filter all happen locally in JavaScript. Nothing you type is uploaded to a server. There is no account, no login and no network request after the page has loaded — the tool works offline once the page is cached. Your input is not stored anywhere; refreshing the page clears it.
Limitations
- The converter handles the two most common encodings — standard 8421 BCD and Excess-3 (XS-3). It does not implement the other variants (2421, 5421, biquinary) that appear in some textbooks.
- Negative numbers and sign nibbles are not supported. A decimal value must be non-negative (or unsigned), and a BCD stream must not contain sign nibbles. COBOL COMP-3 sign-nibble decoding is out of scope.
- Only one decimal point is allowed, and only when it separates integer digits from fractional digits. BCD does not have a native decimal-point representation — the tool keeps the point visible so the value remains unambiguous, but does not encode it as a nibble.
- Batch conversion is line-based. Each non-empty line is treated as one value. Multi-line values are not supported — join them before pasting.
- The reference table covers single-digit codes 0–9. Multi-digit conversions are what the tool itself does; the table is only the base lookup.
- The tool does not perform BCD arithmetic. It converts representation only — you cannot add or subtract two BCD values with it. The "+6 correction" section explains the algorithm if you want to implement it yourself.
Decimal to BCD — frequently asked questions
How do I convert decimal to BCD?
Split the decimal number into individual digits, then replace each digit with its 4-bit binary equivalent from the 8421 table: 0=0000, 1=0001, 2=0010, 3=0011, 4=0100, 5=0101, 6=0110, 7=0111, 8=1000, 9=1001. Join the nibbles in order. For example, decimal 254 becomes 0010 0101 0100.
What is BCD (Binary-Coded Decimal)?
BCD is a class of binary encodings in which each decimal digit is stored as its own fixed 4-bit code. Unlike pure binary which converts the whole number to base 2, BCD encodes each digit independently, so 10 in BCD is 0001 0000 rather than 1010. This makes decimal conversion trivial and eliminates rounding errors in financial arithmetic.
What is the difference between BCD and pure binary?
They are identical for the digits 0–9 but diverge for any multi-digit number. Decimal 45 in pure binary is 101101 (6 bits); the same number in BCD is 0100 0101 (8 bits). BCD always uses 4 bits per decimal digit, so a value like 137 needs 12 bits in BCD but only 8 bits in binary. BCD trades storage density for exact decimal representation.
What are the invalid BCD codes?
Standard 8421 BCD only uses the 4-bit patterns 0000 through 1001. The remaining six patterns — 1010, 1011, 1100, 1101, 1110 and 1111 (decimal 10 to 15) — are invalid in standard BCD. A BCD string containing any of these nibbles is malformed and the tool flags them.
What is packed BCD vs unpacked BCD?
In unpacked BCD, each decimal digit occupies a full byte (8 bits) — only the low nibble carries the digit. In packed BCD, two decimal digits are stored per byte: the more-significant digit in the high nibble, the less-significant in the low nibble. Decimal 42 packed BCD is 0x42 — one byte instead of two.
What is Excess-3 (XS-3) code?
Excess-3 is a variant of BCD where each decimal digit is encoded as its BCD value plus 3. So 0 becomes 0011, 1 becomes 0100, and 9 becomes 1100. XS-3 is self-complementing — the 9's complement of a digit is obtained by inverting every bit — which simplifies subtraction hardware.
Can BCD represent negative numbers?
BCD itself is unsigned. Systems that need signed BCD reserve a nibble for the sign. A common convention uses 1100 (+) and 1101 (−), placed either before or after the digits. COBOL's COMP-3 packed decimal uses this approach — digits in every nibble except the last, and the final nibble holds the sign.
Can BCD represent a decimal point?
Yes. BCD stores each decimal digit individually, so a decimal point can be encoded either by reserving a nibble for its position or by using a fixed-point convention where the point's location is agreed in advance. The value 123.45 with two implied decimal places becomes 0001 0010 0011 0100 0101 in BCD. This tool keeps the decimal point in the output so the value remains unambiguous.
Why is BCD used instead of binary?
BCD is used where exact decimal behaviour matters: financial systems (money arithmetic with no floating-point rounding), digital clocks and calculators (each digit drives one seven-segment display directly), embedded devices with BCD hardware units, real-time clocks (the DS1307 stores time in BCD), and legacy COBOL packed-decimal databases.
How do I add two BCD numbers?
Add the nibbles like ordinary binary, then apply the +6 correction: if the binary sum of any nibble exceeds 9, or if the nibble produces a carry, add 6 (0110) to that nibble to correct it back into the valid 0–9 range. For example, 7 + 5 = binary 1100 (12), which is invalid BCD, so add 6 to get 0010 with a carry out — decimal 12.
Can I convert multiple decimal values at once?
Yes. Enter one value per line and the tool converts each line independently. A batch results table shows the decimal input, the BCD output and the digit count for every value.
Does BCD use more space than binary?
Yes. BCD uses exactly 4 bits per decimal digit, so an n-digit number always needs 4n bits — versus the roughly 3.32 bits per decimal digit that pure binary needs. That is about 20% more storage for the same range, plus 6 of the 16 possible nibbles (1010–1111) go unused in standard BCD.
What is the 8421 code?
8421 code is another name for natural BCD — the four bit positions in each nibble carry weights 8, 4, 2 and 1 from left to right. A digit is the sum of the weights where the corresponding bit is 1. For example, 5 = 4 + 1 = 0101, and 9 = 8 + 1 = 1001.
What is BCD used for in real life?
Digital clocks, wristwatches and calculators (each digit drives a seven-segment display), the real-time clock chips in most computers (e.g. the DS1307 stores the date and time in BCD), COBOL packed-decimal accounting systems, bank transaction ledgers, taxi meters, and industrial instruments that display decimal values.
How does the DS1307 real-time clock store time?
The DS1307 stores each time field as a packed BCD byte. The seconds register holds the tens-of-seconds digit in the high nibble and the ones-of-seconds digit in the low nibble. When you read the register you get, for example, 0x42 = 0100 0010, which decodes to 42 seconds. The same pattern applies to minutes, hours, day, date, month and year.
Why does COBOL use packed decimal instead of binary?
COBOL was designed for business data processing where exact decimal arithmetic is essential — money, quantities, tax calculations. Pure binary floating-point cannot represent 0.10 exactly, and rounding errors compound over millions of transactions. COBOL COMP-3 packed decimal stores two digits per byte with a trailing sign nibble, giving byte-efficient, exactly-representable decimal arithmetic.
Is anything I paste uploaded?
No. The converter runs entirely in your browser using vanilla JavaScript. Nothing you type is uploaded, logged or stored on a server. There is no account, no login and no network request after the page loads — the tool works offline once cached.
Related tools on AIToolsPros
Sources & methodology
- Wikipedia — Binary-coded decimal: reference model for the 8421 code, packed/unpacked layouts, and the 1010–1111 invalid-code set.
- Wikipedia — Excess-3: the self-complementing property and the historical use of XS-3 in early decimal computers.
- NIST Dictionary of Algorithms and Data Structures — Binary-Coded Decimal: authoritative definition of BCD and the two digits-per-byte packed convention.
- IEEE 754-2019 (Standard for Floating-Point Arithmetic): modern successor of BCD used by Densely Packed Decimal (DPD) encoding in decimal floating-point arithmetic.
- ISO/IEC 1989 (COBOL): specification of the
PACKED-DECIMAL/COMP-3storage format — two digits per byte plus a trailing sign nibble. - Analog Devices DS1307 datasheet: documentation of the packed-BCD time and date registers used by the majority of I²C real-time clock chips.
- IBM z/OS documentation — Packed decimal data types: reference for the sign-nibble conventions (0x0C for +, 0x0D for −) used by mainframe packed-decimal fields.
- Texas Instruments SN7447A datasheet: the classic BCD-to-seven-segment decoder used to drive every digit of a numeric display.
- M. Morris Mano, Digital Logic and Computer Design (Pearson): canonical textbook treatment of the 8421 BCD code, invalid codes (1010–1111), and the +6 correction algorithm for BCD addition.