HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Decoding binary coded decimal (BCD) value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
decimalvaluebinarybcddecodingcoded

Problem

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> 4;
        iTmp2 = ucTmp & 0x0f;
        uResult += (iTmp1*10+iTmp2) * static_cast(pow(100.0,static_cast(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] & 0x80)!= 0)
        isNegative = true;

    nResult = data[0] & 0x7f;

    for (unsigned int i=1;i(pow(2.0,static_cast(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<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 po

Solution

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> 4) * 10 ) + ( data[i] & 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;
}

Code Snippets

#include "seatest.h"

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

    for (i=0;i<n;i++) 
    {
        uResult = (uResult * 100) + ((data[i] >> 4) * 10 ) + ( data[i] & 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;
}

Context

StackExchange Code Review Q#2623, answer score: 4

Revisions (0)

No revisions yet.