地磅中读出的数据是压缩的bcd码,请教如何转换为十进制或者是ascii码.最后能有示例程序.谢谢  

解决方案 »

  1.   

    /////////////////////////////////////////////////////////   
    107.//   
    108.// 功能: BCD 转 10 进制   
    109.//   
    110.// 输入: const unsigned char *bcd     待转换的 BCD 码   
    111.//      int length                   BCD 码数据长度   
    112.//   
    113.// 输出:   
    114.//   
    115.// 返回: unsigned long               当前数据位的权   
    116.//   
    117.// 思路:压缩 BCD 码一个字符所表示的十进制数据范围为 0 ~ 99, 进制为 100   
    118.//      先求每个字符所表示的十进制值,然后乘以权   
    119.//////////////////////////////////////////////////////////   
    120.unsigned long  BCDtoDec(const unsigned char *bcd, int length)   
    121.{   
    122.     int i, tmp;   
    123.     unsigned long dec = 0;   
    124.    
    125.     for(i=0; i<length; i++)   
    126.     {   
    127.        tmp = ((bcd[i]>>4)&0x0F)*10 + (bcd[i]&0x0F);      
    128.        dec += tmp * power(100, length-1-i);             
    129.     }   
    130.    
    131.     return dec;   
    132.}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lenyusun/archive/2010/03/05/5350143.aspx
      

  2.   

    unsigned char Hundreds,Tens,Ones;   
    void DecimalToBcdAscii(signed short DecimalValue)   
        {   
        Hundreds = 0;                           // Initialize BCD values    
        Tens = 0;   
        Ones = 0;   
         
        Hundreds:                               // Hundreds    
            DecimalValue = DecimalValue - 100;   
            if (DecimalValue  0)      
                {   
                goto Tens1;   
                }   
            Hundreds = Hundreds + 1;            // Increment Hundreds count    
            goto Hundreds;     
        Tens1:                                  // Tens    
            DecimalValue = DecimalValue + 100;   
        Tens2:   
            DecimalValue = DecimalValue - 10;   
            if (DecimalValue  0)      
                {   
                goto Ones1;   
                }   
            Tens = Tens + 1;                    // Increment Tens count    
            goto Tens2;       
        Ones1:                                  // Ones    
            DecimalValue = DecimalValue + 10;   
        Ones2:   
            DecimalValue = DecimalValue - 1;   
            if (DecimalValue  0)      
                {   
                goto AddAsciiOffset;   
                }   
            Ones = Ones + 1;                    // Increment Ones count    
            goto Ones2;       
        AddAsciiOffset:                         // Add ASCII offset    
            Hundreds = Hundreds + 48;   
            Tens = Tens + 48;   
            Ones = Ones + 48;    
        }