Hex-a-decimal and BCD

A normal decimal digit can have 10 different values, namely 0 - 9.
A hex-a-decimal number (hex means 6) can have 16 different values (base 16).
The values from this numeration system are 0 - 9 and A - F.
The letters correspond with the numbers 10 - 15 according to the decimal numeration system.
This numeration system apply to give binary numbers shorten.

0 0 1 0
1 1 0 1
 = Binary byte (8 bits)
2 + D  = 2D  Hexadecimal number


What is BCD?
B
inary Coded Decimal, is a fourbit code notation for the decimal digits 0 - 9 (base 10).
BCD counts as how we count daily in decimals, only the notation is now binary.
The number 31 for example is binary 00011111, in BCD 0011 0001 (the '3' stays here on the left, the '1' on the right side) see also the table.

Converting from BCD to decimal in PIC Basic as follows:

  MyByte = ((MyBCD >> 4 ) * 10 ) + (MyBCD & 15 )

Or, if it must be the same variable which must be converted:

  MyByte = ((MyByte >> 4 ) * 10 ) + (MyByte & 15 )

 

Converting from decimal to BCD in PIC Basic as follows:

  MyBCD = ((MyByte / 10 ) << 4 ) + (MyByte // 10 )

Or, if it must be the same variable which must be converted:

  MyByte = ((MyByte / 10 ) << 4 ) + (MyByte // 10 )

 


Decimal  Hex Binair BCD
0   00 00000000 0000 0000
1   01 00000001 0000 0001
2   02 00000010 0000 0010
3   03 00000011 0000 0011
4   04 00000100 0000 0100
5   05 00000101 0000 0101
6   06 00000110 0000 0110
7   07 00000111 0000 0111
8   08 00001000 0000 1000
9   09 00001001 0000 1001
10   0A 00001010 0001 0000
11   0B 00001011 0001 0001
12   0C 00001100 0001 0010
13   0D 00001101 0001 0011
14   0E 00001110 0001 0100
15   0F 00001111 0001 0101
16   10 00010000 0001 0110
17   11 00010001 0001 0111
18   12 00010010 0001 1000
19   13 00010011 0001 1001
20   14 00010100 0010 0000
21   15 00010101 0010 0001
22   16 00010110 0010 0010
23   17 00010111 0010 0011
24   18 00011000 0010 0100
25   19 00011001 0010 0101
26   1A 00011010 0010 0110
27   1B 00011011 0010 0111
28   1C 00011100 0010 1000
29   1D 00011101 0010 1001
30   1E 00011110 0011 0000
31   1F 00011111 0011 0001
32   20 00100000 0011 0010
33   21 00100001 0011 0011
etc. etc. etc. etc.