C/C++教程

bcd处理小源码

本文主要是介绍bcd处理小源码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

bcd处理小源码

#include <string>
#include <stdint.h>
void BCD2ASC(unsigned char *asc, const unsigned char *bcd, int len)
{
    static unsigned char bcd2ascii[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    static unsigned char ascii2bcd1[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    static unsigned char ascii2bcd2[6] = { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
    unsigned char c = 0;
    unsigned char i;
    for (i = 0; i < len; i++) {
        //first BCD  
        c = *bcd >> 4;
        *asc++ = bcd2ascii[c];

        //second  
        c = *bcd & 0x0f;
        *asc++ = bcd2ascii[c];
        bcd++;
    }
}


static std::string BCD2ASC( const unsigned char *bcd, int len)
{
    static unsigned char bcd2ascii[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    static unsigned char ascii2bcd1[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    static unsigned char ascii2bcd2[6] = { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
    unsigned char c = 0;
    unsigned char i;
    std::string asc;
    for (i = 0; i < len; i++) {
        //first BCD
        c = *bcd >> 4;
        asc.push_back( bcd2ascii[c]);

        //second
        c = *bcd & 0x0f;
        asc.push_back( bcd2ascii[c]);
        bcd++;
    }
    return asc;
}


#include <stdio.h>
int main(int argc, char const *argv[])
{
    // unsigned char bcd[]={0x33,0xFF,0x55};

    // unsigned char asc[100]={0};
    // BCD2ASC(asc,bcd,sizeof(bcd));
   
    // auto ch=BCD2ASC(bcd,sizeof(bcd));

    // printf("%s\r\n",asc);
    // printf("%s\r\n",ch.c_str());


    const int POS_ADDR = 14;
    const int POS_VERSION = 47;
    uint8_t data[]={0x68,0x38,0x00,0xC1,0x01,0x00,0x00,0x00,0x00,0x00,0x03,0x02,0x01,0x51,0x30,0x00,0x00,0x00,0x00,0x28,0x28,0x00,0x80,0x04,0x80,0x04,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0xD0,0x07,0x00,0x00,0x21,0x03,0x13,0x21,0x03,0x13,0x59,0x52,0x54,0x41,0x30,0x06,0x21,0x99,0x01,0x00,0x00,0x07,0x16};

    std::string master_ver;
    master_ver.clear();
    static unsigned char bcd2ascii[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    unsigned char * bcd=(uint8_t *)(data + POS_VERSION+2);
    master_ver.push_back( bcd2ascii[*bcd >> 4]);
    master_ver.push_back( bcd2ascii[*bcd & 0x0f ]);
    bcd=(uint8_t *)(data + POS_VERSION+1);
    master_ver.push_back( bcd2ascii[*bcd >> 4]);
    master_ver.push_back( bcd2ascii[*bcd & 0x0f ]);
    bcd=data + POS_VERSION;
    master_ver.push_back( bcd2ascii[*bcd >> 4]);
    master_ver.push_back( bcd2ascii[*bcd & 0x0f ]);
    bcd=data + POS_VERSION+3;
    master_ver.push_back( bcd2ascii[*bcd >> 4]);
    master_ver.push_back( bcd2ascii[*bcd & 0x0f ]);
    bcd=data + POS_VERSION+4;
    master_ver.push_back( bcd2ascii[*bcd >> 4]);
    master_ver.push_back( bcd2ascii[*bcd & 0x0f ]);
    printf("%s\r\n",master_ver.c_str());
    return 0;
}
这篇关于bcd处理小源码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!