ASC转换BCD,ASC2BCD(转)

int ASC2BCD(const char* szASC,byte* szBDC)

 

{

 

int szASCLen=strlen(szASC);

 

byte * bpBCD = new byte[szASCLen/2];

 

int nss=0,nOu=0;

 

for (int x=szASCLen-1;x>=0;x–)

 

{

 

//取出字符串中的一个数值

 

char char_1=*(szASC+x);

 

//取出BCD码

 

int nBDC;

 

if (char_1 != ‘.’) nBDC=(char_1 & 0xF);

 

else nBDC=14;

 
 

 

if ((++nOu)&0x01)

 

bpBCD[nss]=(nBDC << 4);

 

else

 

bpBCD[nss++] += nBDC;

 
 

 

}

 

int nBit=0;

 

if (nOu%2==0) nBit=nOu/2-1;

 

else nBit=nOu/2;

 

for (int y=0;y<=nOu/2;y++)

 

{

 

szBDC[y]=(bpBCD[nBit-y] << 4);

 

szBDC[y]+=(bpBCD[nBit-y] >> 4);

 

}

 

return nBit;

 
 

 

// memcpy(szBDC,bpBCD,sizeof(bpBCD)*2);

 

}

 

void CDotTestDlg::OnButton1()

 

{

 

char szTest[]={“123456789123456789.789”} ;

 
 

 

int s=strlen(szTest);

 
 

 

byte * szAn=new byte[s/2];

 

int w=ASC2BCD(szTest,szAn);

 

for(int x=0;x<=w;)

 

TRACE(“%x “,szAn[x++]);

 

}

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

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注