ASCII码转BCD码
- @param in AscBuf 需进行转换的ASCII码数据
- @param in AscLen 传入的ASCII码数据长度,即转化BCD码数据的双倍长度
- @param out BcdBuf 转换输出的BCD码数据
- @return
- @li UUTIL_SUCCESS = 0 成功
- @li UUTIL_FAIL = -1, 失败
- @补充说明: 左靠BCD码,位数不足后补‘F’
int Util_Asc2Bcd(char *AscBuf, char *BcdBuf, int AscLen)
{
int i, nAscLen, len;
const char *pAsc = AscBuf;
unsigned char *pBcd = BcdBuf;
if (pAsc == NULL || pBcd == NULL || AscLen < 0)
{
return APP_FAIL;
}
//获取ASCII 字符串长度
nAscLen = AscLen;
for (i = 0; i < nAscLen; i++)
{
if ((pAsc[i] >= '0' && pAsc[i] <= '9')
|| (pAsc[i] >= 'a' && pAsc[i] <= 'f')
|| (pAsc[i] >= 'A' && pAsc[i] <= 'F'))
{
continue;
}
break;
}
if (i < nAscLen) nAscLen = i;
if (IsAscForHex(pAsc, nAscLen) < 0)
{
return APP_FAIL;
}
len = nAscLen / 2;
for (i = 0; i < len; i++)
{
pBcd[i] = ((pAsc[i * 2] <= 0x39) ? pAsc[i * 2] - 0x30 //若值为0~9
: ((pAsc[i * 2] >= 0x61) ? pAsc[i * 2] - 0x20 - 0x41 + 10 //若值为小写字母
: pAsc[i * 2] - 0x41 + 10)); //若值为大写字母
pBcd[i] = pBcd[i] << 4;
pBcd[i] += ((pAsc[i * 2 + 1] <= 0x39) ? pAsc[i * 2 + 1] - 0x30//若值为0~9
: ((pAsc[i * 2 + 1] >= 0x61) ? pAsc[i * 2 + 1] - 0x20 - 0x41 + 10 //若值为小写字母
: pAsc[i * 2 + 1] - 0x41 + 10)); //若值为大写字母
}
if (nAscLen % 2 != 0)
{
pBcd[i] = ((pAsc[i * 2] <= 0x39) ? pAsc[i * 2] - 0x30 //若值为0~9
: ((pAsc[i * 2] >= 0x61) ? pAsc[i * 2] - 0x20 - 0x41 + 10 //若值为小写字母
: pAsc[i * 2] - 0x41 + 10)); //若值为大写字母
pBcd[i] = pBcd[i] << 4;
pBcd[i] |= 0x0F;
i++;
}
//如果字符数不足
if (nAscLen < AscLen&&nAscLen != 0)
{
memset(pBcd + i, 0xFF, AscLen - i);
}
return APP_SUCC;
}
BCD码转ASCII码
- @param in BcdBuf 需进行转换的BCD码数据
- @param in AscLen ASCII码数据长度,即BCD码数据的双倍长度
- @param out AscBuf 转换输出的ASCII码数据
- @return
- @li UUTIL_SUCCESS = 0 成功
- @li UUTIL_FAIL = -1, 失败
int Util_Bcd2Asc(char *BcdBuf, char *AscBuf, int AscLen)
{
int i;
char ch;
const unsigned char *pBcd = BcdBuf;
char *pAsc = AscBuf;
//AscLen长度为奇数则报错
if ((pAsc == NULL) || (pBcd == NULL) || (AscLen < 0))
return APP_FAIL;
for (i = 0; i < AscLen / 2; i++)
{
ch = (pBcd[i] & 0xF0) >> 4;
pAsc[i * 2] = (ch > 9) ? ch + 0x41 - 10 : ch + 0x30;
ch = pBcd[i] & 0x0F;
pAsc[i * 2 + 1] = (ch > 9) ? ch + 0x41 - 10 : ch + 0x30;
}
if (AscLen % 2)//当长度为奇数时需处理
{
ch = (pBcd[i] & 0xF0) >> 4;
pAsc[i * 2] = (ch > 9) ? ch + 0x41 - 10 : ch + 0x30;
}
if (IsAscForHex(pAsc, AscLen) < 0)
{
return APP_FAIL;
}
return APP_SUCC;
}
int型数据转换为BCD码
右靠BCD码,位数不足左补0
*参数说明:
输入参数: IntData: 待转换的int型数据
BcdLen: 转换后BCD码数据长度
输出参数: BcdBuf: 转换后BCD数据
*返 回 值: UUTIL_FAIL = -1, //失败
UUTIL_SUCCESS = 0 //成功
int Util_Int2Bcd(unsigned int IntData, char *BcdBuf, int BcdLen)
{
int iRet;
char temp[20];
char *pBcdbuf;
unsigned int bcdBufSize = BcdLen * 2;
if (BcdBuf == NULL)
{
return APP_FAIL;
}
pBcdbuf = (char *)malloc(bcdBufSize);
if (pBcdbuf == NULL)
{
return APP_FAIL;
}
memset(pBcdbuf, '0', bcdBufSize);
memset(temp, 0, sizeof(temp));
sprintf(temp, "%u", IntData);
if (bcdBufSize < strlen(temp))
{
free(pBcdbuf);
return APP_FAIL;
}
memcpy(pBcdbuf + bcdBufSize - strlen(temp), temp, strlen(temp));
iRet = Util_Asc2Bcd(pBcdbuf, (unsigned char *)BcdBuf, bcdBufSize);
free(pBcdbuf);
if (iRet < 0)
{
return APP_FAIL;
}
return APP_SUCC;
}