BCD Converter
BCD ↔ decimal with per-nibble validation and error position.
This tool converts packed BCD words to decimal and decimal values to BCD: 0x1234 ↔ 1234. Every nibble is validated — a word like 0x12A4 is rejected with the offending digit highlighted, because 0xA is not a decimal digit.
How it works
Packed BCD encodes one decimal digit per 4-bit nibble, most significant digit first. Decoding walks the nibbles left to right, multiplying the accumulator by ten; any nibble above 9 aborts with its position. Encoding is the reverse: peel decimal digits off with division by ten and pack each into 4 bits. The BCD representation of a number is therefore identical to its decimal digits read as hex — which is exactly what makes accidental binary/BCD mix-ups so easy to miss.
Worked example
5678 → 0x5678
0x12A4 → invalid — nibble 2 is 0xA (must be 0–9)
Parameters
| Parameter | Value | Notes |
|---|---|---|
| Encoding | packed BCD | 1 decimal digit per nibble |
| Digit range | 0 – 9 per nibble | 0xA–0xF invalid |
| Capacity | 8 digits / 32 bits | 0 … 99 999 999 |
| Error report | nibble index from MSB | 0-based |
C implementation
#include <stdint.h>
/* Packed BCD word -> decimal; returns -1 on an invalid nibble */
long bcd_to_dec(uint32_t bcd, int nibbles)
{
long value = 0;
for (int i = nibbles - 1; i >= 0; i--) {
uint32_t nib = (bcd >> (i * 4)) & 0xF;
if (nib > 9) return -1; /* e.g. 0x12A4 fails here */
value = value * 10 + (long)nib;
}
return value; /* 0x1234 -> 1234 */
}FAQ
▸What is packed BCD?
Binary-coded decimal stores one decimal digit (0–9) in each 4-bit nibble. The hex word 0x1234 is literally the digits 1, 2, 3, 4 — decimal 1234. Older PLC function blocks, thumbwheel switches and 7-segment display drivers all speak BCD.
▸Why is 0x12A4 invalid?
Its third nibble (position 2, counting from the most significant) is 0xA — greater than 9, which no decimal digit maps to. The converter highlights exactly that nibble instead of silently producing a wrong number.
▸How is BCD different from plain hex?
In BCD, 0x1234 means decimal 1234. As a plain binary number, 0x1234 is 4660. Reading a BCD register as binary (or vice versa) is a classic PLC integration bug — if your values are consistently wrong in a pattern, check for exactly this.
▸What range can be converted?
Up to 8 digits (a 32-bit word): decimal 0 to 99 999 999. Each digit costs 4 bits, so a 16-bit word holds 4 digits, a 32-bit word 8.
▸Is my data uploaded?
No. Conversion runs locally in your browser.