ASC转换BCD,ASC2BCD(转)

  1. int ASC2BCD(const char* szASC,byte* szBDC)
  2.  
    {
  3.  
    int szASCLen=strlen(szASC);
  4.  
    byte * bpBCD = new byte[szASCLen/2];
  5.  
    int nss=0,nOu=0;
  6.  
    for (int x=szASCLen-1;x>=0;x--)
  7.  
    {
  8.  
    //取出字符串中的一个数值
  9.  
    char char_1=*(szASC+x);
  10.  
    //取出BCD码
  11.  
    int nBDC;
  12.  
    if (char_1 != '.') nBDC=(char_1 & 0xF);
  13.  
    else nBDC=14;
  14.  
     
  15.  
    if ((++nOu)&0x01)
  16.  
    bpBCD[nss]=(nBDC << 4);
  17.  
    else
  18.  
    bpBCD[nss++] += nBDC;
  19.  
     
  20.  
    }
  21.  
    int nBit=0;
  22.  
    if (nOu%2==0) nBit=nOu/2-1;
  23.  
    else nBit=nOu/2;
  24.  
    for (int y=0;y<=nOu/2;y++)
  25.  
    {
  26.  
    szBDC[y]=(bpBCD[nBit-y] << 4);
  27.  
    szBDC[y]+=(bpBCD[nBit-y] >> 4);
  28.  
    }
  29.  
    return nBit;
  30.  
     
  31.  
    // memcpy(szBDC,bpBCD,sizeof(bpBCD)*2);
  32.  
    }
  1.  
    void CDotTestDlg::OnButton1()
  2.  
    {
  3.  
    char szTest[]={"123456789123456789.789"} ;
  4.  
     
  5.  
    int s=strlen(szTest);
  6.  
     
  7.  
    byte * szAn=new byte[s/2];
  8.  
    int w=ASC2BCD(szTest,szAn);
  9.  
    for(int x=0;x<=w;)
  10.  
    TRACE("%x ",szAn[x++]);
  11.  
    }



事例:
输出:12 34 56 78 91 23 45 67 89 e7 89

原文地址:https://www.cnblogs.com/Pond-ZZC/p/9259769.html