• 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
    ",asc);
        // printf("%s
    ",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
    ",master_ver.c_str());
        return 0;
    }
    
  • 相关阅读:
    touch测试
    JS动画代码
    前端css、javascript在线工具
    横向广告(商品)滚动
    写点js的小函数(一)
    HTML5 css reset
    JS新API标准 地理定位(navigator.geolocation)
    写点js的小函数(二、文本框的提示)
    传说中的comet(ajax版)?
    lhgdialog 4.2.0 正式版发布
  • 原文地址:https://www.cnblogs.com/zongzi10010/p/15430384.html
Copyright © 2020-2023  润新知