Binary coded decimal (BCD) to Hexadecimal conversion

iPadDevloperJr picture iPadDevloperJr · Dec 20, 2010 · Viewed 69.5k times · Source

can someone explain to me how to convert BCD to Hexadecimal? For example how can i convert 98(BCD) to Hexadecimal. Thanks.

Answer

Lukasz picture Lukasz · Dec 20, 2010

I don't quite understand your question, but I'm guessing that e.g. someone gives you a number 98 encoded in BCD, which would be:

1001 1000

and you are supposed to get:

62H

What I would propose:

1) convert BCD-encoded value to decimal value (D)

2) convert D to hexadecimal value.

Depending on which programming language you choose, this task will be easier or harder.

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)
    );