Binary Coded Decimal (BCD) to Hexadecimal Conversion

can someone explain to me how to convert BCD to hex? For example, how can I convert 98 (BCD) to hexadecimal. Thank.

+5
source share
5 answers

I do not quite understand your question, but I assume that, for example, someone gives you the number 98, encoded in BCD, which will be:

1001 1000

and you should get:

62H

What I suggest:

1) converts a BCD encoded value to a decimal value (D)

2) convert D to hexadecimal value.

Depending on your chosen programming language, this task will be simpler or more complicated.

EDIT: in Java, it could be:

    byte bcd = (byte)0x98; // BCD value: 1001 1000

    int decimal = (bcd & 0xF) + (((int)bcd & 0xF0) >> 4)*10;

    System.out.println(
            Integer.toHexString(decimal)
    );
+8

BCD - , - BCD . , "98" BCD 10011000, 98

+9

BCD- ( int).

:

unsigned int bcd2dec(unsigned int bcd)
{
    unsigned int dec=0;
    unsigned int mult;
    for (mult=1; bcd; bcd=bcd>>4,mult*=10)
    {
        dec += (bcd & 0x0f) * mult;
    }
    return dec;
}

:

unsigned int bcd2dec_r(unsigned int bcd)
{
    return bcd ? (bcd2dec_r(bcd>>4)*10) + (bcd & 0x0f) : 0; 
}
+3

256 , BCD ; .

+2

Switch between different combinations of hexadecimal, decimal and binary. If you know how binary files work, you should easily use BCD:

Hex  Dec  BCD
0    0    0000
1    1    0001
2    2    0010
3    3    0011
4    4    0100
5    5    0101
6    6    0110
7    7    0111
8    8    1000
9    9    1001
A   10    0001 0000 <-- notice that each digit looks like hex except it can only go to 9.
B   11    0001 0001
C   12    0001 0010
D   13    0001 0011
E   14    0001 0100
F   15    0001 0101

Once you get this part, you can use the division by 10 or% 10 to find any combination to generate your BCD. Since it uses only 10 combinations instead of all 16, you will lose some information.

+2
source

All Articles