ASCII to BCD Converter
Convert ASCII text to BCD (Binary-Coded Decimal) and back. Supports packed and unpacked BCD formats with hex, binary, and decimal output.
About
Binary-Coded Decimal (BCD) encodes each decimal digit of a number into a fixed 4-bit binary nibble. Unlike pure binary, BCD preserves decimal digit boundaries, which matters in financial systems, embedded controllers, and legacy mainframe protocols (IBM System/360 used packed BCD natively). Misinterpreting packed vs. unpacked format corrupts data silently - a 0xF padding nibble read as a digit produces garbage. This tool converts ASCII character codes into their BCD representations (both packed and unpacked) and reverses the process. It assumes standard printable ASCII range (0x20 - 0x7E). Note: BCD only represents decimal digits 0 - 9, so each ASCII code is first decomposed into its constituent decimal digits before encoding.
Packed BCD stores two digits per byte, halving storage compared to unpacked format, but requires careful handling of odd-length digit strings (this tool pads with 0xF as per IBM convention). Unpacked BCD zeros the upper nibble of each byte, wasting space but simplifying per-digit access. This tool approximates standard BCD encoding conventions and does not handle sign nibbles (0xC/0xD) used in COBOL signed fields.
Formulas
BCD encoding decomposes each ASCII character's decimal code into individual digits, then maps each digit to a 4-bit nibble.
For a character with ASCII code c, extract the decimal digits:
digits = decimalDigits(c)
Unpacked BCD: each digit di maps to one byte:
bytei = 0000 di (binary), i.e., upper nibble = 0x0, lower nibble = di
Packed BCD: pair digits into bytes:
bytej = (d2j << 4) | d2j+1
If the total digit count is odd, the final nibble is padded with 0xF:
bytelast = (dn << 4) | 0xF
Where c = ASCII character code (decimal), di = the i-th decimal digit of c, 0xF = padding nibble (1111 binary) per IBM packed decimal convention, and << = bitwise left shift operator.
Reference Data
| ASCII Char | Decimal Code | Hex Code | Decimal Digits | Unpacked BCD (Hex) | Packed BCD (Hex) |
|---|---|---|---|---|---|
| 0 | 48 | 0x30 | 4, 8 | 04 08 | 48 |
| 1 | 49 | 0x31 | 4, 9 | 04 09 | 49 |
| 9 | 57 | 0x39 | 5, 7 | 05 07 | 57 |
| A | 65 | 0x41 | 6, 5 | 06 05 | 65 |
| B | 66 | 0x42 | 6, 6 | 06 06 | 66 |
| Z | 90 | 0x5A | 9, 0 | 09 00 | 90 |
| a | 97 | 0x61 | 9, 7 | 09 07 | 97 |
| z | 122 | 0x7A | 1, 2, 2 | 01 02 02 | 12 2F |
| Space | 32 | 0x20 | 3, 2 | 03 02 | 32 |
| ! | 33 | 0x21 | 3, 3 | 03 03 | 33 |
| + | 43 | 0x2B | 4, 3 | 04 03 | 43 |
| . | 46 | 0x2E | 4, 6 | 04 06 | 46 |
| / | 47 | 0x2F | 4, 7 | 04 07 | 47 |
| @ | 64 | 0x40 | 6, 4 | 06 04 | 64 |
| [ | 91 | 0x5B | 9, 1 | 09 01 | 91 |
| ~ | 126 | 0x7E | 1, 2, 6 | 01 02 06 | 12 6F |
| { | 123 | 0x7B | 1, 2, 3 | 01 02 03 | 12 3F |
| } | 125 | 0x7D | 1, 2, 5 | 01 02 05 | 12 5F |
| % | 37 | 0x25 | 3, 7 | 03 07 | 37 |
| # | 35 | 0x23 | 3, 5 | 03 05 | 35 |