Decoding binary coded decimal (BCD) value
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;
}