void str_to_bcd(char *p_sourse,char *p_dest,int d_len,int dig_len)
{
unsigned char _buf[20];
unsigned char *p_e = (unsigned char *)p_dest;
bool carry = false;
int *_s = (int *)_buf;
while(_s<(int *)_buf + 5) *_s++ =0;
int i = 0,j=0;
for(;p_sourse[i];i++){};
while(p_sourse[j++]){//j 整数位数
if(p_sourse[j] == '.' || p_sourse[j] == 0) break;
}
//四舍五入
if(i-j-1 > dig_len){
if(p_sourse[j+dig_len+1]>='5'){
int m = j+dig_len;
for(;m>=0;m--)
if(p_sourse[m] < '9' && p_sourse[m] != '.') break;
if(m>-1 || j < 2*d_len - dig_len){
++p_sourse[m];
for(int n = m+1;n<=j+dig_len+1;n++) p_sourse[n] = 0;
carry = m == -1;
}
}
}
if(j> 2*d_len - dig_len) return;
int itg = 2*d_len -dig_len - j;
if(carry) _buf[itg-1] = 1;
for(int t=0;t<j;t++) _buf[itg++] = p_sourse[t];//填写整数部分
if(i-j>=2){
for(int c = 0;c<i-j-1;c++) _buf[itg++] = p_sourse[j+c+1];//小数部分
}
for(int k = 2*d_len;k>0;k -= 2){
*p_e++ = _buf[k-1]&0xf | _buf[k-2]<<4;
}
}