Decoding binary coded decimal (BCD) value I have 3 questions, all of them are concerned with binary coded decimal (BCD) conversion. My coding snippets are listed as follows. But I don't like my algorithm because it takes too long when dealing with tons of data.

Any suggestions to improve the performance? Would you please show me an efficient and fast way to do that?


The value is expressed with 4-bits binary coded decimals (BCD), which was originally stored in a character buffer (for example, pointed by a pointer const unsigned char *).  


BCD*2; 1001 0111 0110 0101=9765
"9"  "7"  "6"  "5"



unsigned int BCDn( unsigned int n, const unsigned char * data )
{
    unsigned int uResult = 0;
    unsigned char ucTmp;
    int iTmp1,iTmp2;

    for (unsigned int i=0;i&lt;n;i++)
    {
        ucTmp = data[i];
        iTmp1 = (ucTmp &amp; 0xf0) &gt;&gt; 4;
        iTmp2 = ucTmp &amp; 0x0f;
        uResult += (iTmp1*10+iTmp2) * static_cast&lt;unsigned int&gt;(pow(100.0,static_cast&lt;int&gt;(n-1-i)));
    }

    return uResult; 
}

The value is expressed by n binary integer (n* 8-bits). The first bit (MSB) defines
the sign of binary integer; "0" signifies that it's a positive integer, and "1" for negative one. In the case of negative number, the other bits show the complement number that is added 1.


         MSB          LSB
    I*2; 00101101 1001100=19999
    I*2; 10101101 10011100=(-1)*(0101101 10011100 
                          =(-1)*(1010010 01100100) (after complement)
                          =-21092





int SINTn(unsigned int n, const unsigned char *data)
{   
    int nResult;
    bool bNegative = false;

    if ((data[0] &amp; 0x80)!= 0)
        isNegative = true;

    nResult = data[0] &amp; 0x7f;

    for (unsigned int i=1;i&lt;n;i++)
        nResult = nResult * 0x100 + data[i];

    if (bNegative) 
        nResult = nResult - static_cast&lt;int&gt;(pow(2.0,static_cast&lt;int&gt;(n*8-1)));

    return nResult
}

unsigned int UINTn(unsigned int n, const unsigned char *data)
{   
    unsigned int uResult = 0;

    for (unsigned int i=0;i&lt;n;i++)
        uResult = uResult * 0x100 + data[i];

    return uResult;
}

R*n.m

The value is expressed by n-bytes (n*8 bits) binary number, the first bit (MSB)
defines the sign of it; "0" means positive and "1" means negative. The number m means
that the binary number should be multiplied by 10-m to get the value.




           MSB                        LSB
    R*4.0: 00000000 00000000 00000111 10110101=1973
    R*4.2: 00000000 00000000 00000111 10110101=1973*10-2=19.73
    R*4.5: 10000000 00000000 00000111 10110101=-1973*10-5= -0.01973
    R*2.0: 10101101 10011100 =-11676





double REALnm(unsigned int n, unsigned int m, const unsigned char * data)
{   
    double dResult;
    bool bNegative = false;

    if ((data[0] &amp; 0x80)!= 0)
        isNegative = true;

    dResult = data[0] &amp; 0x7f;

    for (unsigned int i=1;i&lt;n;i++)
        dResult = dResult * 0x100 + data[i];

    if (bNegative) dResult *= -1.0;

    dResult *= pow(10.0, (-1.0) * m);

    return dResult;
}
it can be a lot simpler...

#include "seatest.h"

unsigned int BCDn( unsigned int n, const unsigned char * data )
{
    unsigned int uResult = 0;
    unsigned int i;

    for (i=0;i&lt;n;i++) 
    {
        uResult = (uResult * 100) + ((data[i] &gt;&gt; 4) * 10 ) + ( data[i] &amp; 0x0F);     
    }

    return uResult; 
}
void test_max_bcd_convert()
{   
    unsigned char bcd_data[]= { 0x97, 0x65 };

    assert_int_equal(9765, BCDn(2, bcd_data));

}

void test_fixture_bcd( void )
{
    test_fixture_start();      
    run_test(test_max_bcd_convert);   
    test_fixture_end();       
}

void all_tests( void )
{
    test_fixture_bcd();   
}

int main( int argc, char** argv )
{
    run_tests(all_tests);   
    return 0;
}