9

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

AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
iPadDevloperJr
  • 992
  • 3
  • 20
  • 34

5 Answers5

11

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)
    );
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Lukasz
  • 7,572
  • 4
  • 41
  • 50
  • So which algorithme can i use to accomplish this task? – iPadDevloperJr Dec 20 '10 at 22:56
  • @Gallois: What would be the format of the program input (the BCD value)? String? – Lukasz Dec 20 '10 at 23:03
  • The BCD value is a BYTE(8 bits) (HHHHLLLL) – iPadDevloperJr Dec 20 '10 at 23:05
  • ok, Thanks but can you explain to me what you are doing why you shuft 4 bits and do a multiplication by 10? – iPadDevloperJr Dec 20 '10 at 23:24
  • @Gallois: The trick is to separate HHHH value from LLLL value - that's why there is logical AND performed. Then, the HHHH value must be multiplied by 10 because it is the higher nibble (it is the one that represents 'ten's) and both values are added. After you have the result stored in decimal variable, we can easily print it as Hexadecimal value using Java API (Integer.toHexString(...)) – Lukasz Dec 20 '10 at 23:34
10

BCD is a subset of hexadecimal, so there is no conversion necessary -- any given BCD value is identical to the corresponding hexadecimal value. For example, '98' in BCD is 10011000, which is the same as 98 in hexadecimal

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 9
    That's not true. `1001 1000` is the BCD representation of 98, but the binary representation of 98 is `0110 0010`, which is 62 in Hex. Maybe I'm just misinterpreting the question... – AgentConundrum Dec 20 '10 at 22:50
  • 2
    @Agent -- the binary representation of hex 98 is 1001 1000. 0110 0010 is the binary representation of DECIMAL 98, which is completely different from hex 98 – Chris Dodd Dec 25 '10 at 08:19
  • 2
    @ChrisDodd hexadecimal `98` **is** `10011000`, but rather in BCH (binary coded hex), because you cant encode hexadecimal in the short list of BCD codes (no representation for ciphers A-F). The op asks for conversion **from** BCD to hexadecimal. Therefore `10011000` (BCD) should give hexadecimal value `62` (hex), because `10011000` is binary encoded decimal and not binary encoded hex. – mip Jan 30 '14 at 09:34
  • And in terms of binary representation `0001 0101` BCD (decimal `15`) should be converted to `1111` BCH (hexadecimal `F`). If no conversion were necessary then you would end up with hexadecimal `15`, which might have fooled you, but the input was decimal `15`. – mip Jan 30 '14 at 10:12
  • 2
    The answer is completely wrong according what OP wants. – mazhar islam Oct 26 '15 at 06:35
3

For any BCD encoded value (that will fit in an int).

Iterative:

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

Recursive:

unsigned int bcd2dec_r(unsigned int bcd)
{
    return bcd ? (bcd2dec_r(bcd>>4)*10) + (bcd & 0x0f) : 0; 
}
Simon Peverett
  • 4,128
  • 3
  • 32
  • 37
2

I'd create a table of 256 entries, mapping all BCD bytes into their binary equivalent; you can then use hex printing of your programming language.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
2

Go between different combinations of Hex, Decimal, and Binary. If you know how binary works then you should easily be able to 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 got this part of it down, you should be able to use divide by 10 or %10 to find any combination to generate your BCD. Since it only uses 10 combinations instead of all 16 you will lose some information.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Cyber Slueth Omega
  • 399
  • 1
  • 3
  • 19