Talk:Binary-coded decimal/Archives/2020/January

Is there an article about an algorithm that converts a binary value to BCD?
for example: convert the value 11111111 (binary) to 255 with each digit stored in separate bytes:

What I can do is: you simply divide by 10 repeatedly and each time, you use the remainder to place your digits, while the quotient (rounded down) is the next digit to be divided again. For example:


 * 1) 255/10 = Q:25 R:5 That is 5 would be placed in the ones place of the digit BCD storage (XX5).
 * 2) with the quotient in hand, divide by 10 again, which would be 25/10 = Q:2 R:5 store the remainder into the tens place (X55)
 * 3) again, with the new quotient in hand, divide by 10 again: 2/10 = Q:0 R:2 now write the remainder value in the hundreds place (255).

This only works with unsigned integers. For signed integers, simply flip the negative value to a positive (-5 turns into 5, for example), call the routine to write the digits in, and then place a minus sign before it (so 11111111 (255), XOR +1 would be 00000001, then BCD it, which spits out 1 in decimal, and place the minus sign to form -1). You can keep going based on how many digits you want to display.

I did this on Super Mario World ROM hacking when Akaginite developed the 32-bit/16-bit division. Joeleoj123 (talk) 21:49, 24 February 2018 (UTC)

Edit: Included how to convert. Joeleoj123 (talk) 00:38, 8 March 2018 (UTC)


 * I'm sorry... I thought I replied to this. I was certainly intending to. In general we don't include algorithms, particularly not if they're obvious or very well known. This one is widely covered in the literature, eg places like IBM's "Commercial Subroutine Package" (which dates from the 1960s) and, well, this one is obvious (or should be) to anyone in a first-year programming course and so has been "invented" countless times already. Jeh (talk) 03:12, 8 March 2018 (UTC)

BCD code B
I was reading MAX7219/MAX7221 datasheet, and they mention code B, https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf page 7 and 8.

It goes 0-9, then '-', 'E', 'H', 'L', 'P', ' '. Instead of pure A B C D E F. This is because these letter and symbols are more useful in userfacing equipement, i.e. E error, H/L - high low, and '-' for a minus sign in front or between digits, and blank for spacing. P is probably standing for program, i.e. P001, and all 6 characters can be easily displayed on standard 7 segment display in upper case without problem. (B and D cannot be displayed without confusion with 8 and 0).

I do not know if this is industry standard. 81.6.34.246 (talk) 14:21, 31 July 2018 (UTC)


 * It isn't. Neither are A through F (which btw can be displayed just fine using lower case for b and d). And it's really only marginally associated with the article topic, since neither this set of six characters nor A through F are decimal digits. Jeh (talk) 17:48, 31 July 2018 (UTC)


 * The SN7447 (maybe closest to industry standard) implements them with minimal logic, except for the requirement that B'1111' give all segments off. This gives strange looking characters. The DM9368 generates AbCdEF. I believe it uses a ROM, instead of minimized random logic. (It also has current source outputs for directly driving LEDs.) Gah4 (talk) 18:24, 31 July 2018 (UTC)


 * I wonder what CD players do with non-decimal digits in the track number? Gah4 (talk) 18:24, 31 July 2018 (UTC)