我正在对我们系统中的旧 c 代码进行错误修复(奇怪的是,它不是标准 c。它不是由 gcc 编译的)。我来到这个代码,它似乎正在将字符串转换为无符号字符格式的二进制代码。逻辑让我感到困惑(粗体部分)。这对你们有意义吗?
我需要理解这段代码,因为我必须复制它并在另一个长度不是 13 而是 11 的字符串上重用它。
char l_call_dest_no[25];
int l_loop_cnt;
unsigned char l_bcd_byte;
unsigned char l_call_dest_no_in_bcd[13];
...some other code as input...
for (l_loop_cnt = 0; l_loop_cnt < 13; l_loop_cnt++)
{
l_bcd_byte = '\0';
switch (l_call_dest_no[l_loop_cnt * 2])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
l_bcd_byte = (l_call_dest_no[l_loop_cnt * 2] - 48) * 16;
break;
case 'A':
l_bcd_byte = 10 * 16;
break;
case 'B':
l_bcd_byte = 11 * 16;
break;
case 'C':
l_bcd_byte = 12 * 16;
break;
case 'D':
l_bcd_byte = 13 * 16;
break;
case 'E':
l_bcd_byte = 14 * 16;
break;
case 'F':
case ' ':
l_bcd_byte = 15 * 16;
break;
default:
printf("*** invalid call destination number ***\n");
return_status = FAILURE;
break;
}
if (l_loop_cnt < 12)
{
switch (l_call_dest_no[l_loop_cnt * 2 + 1])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
l_bcd_byte = l_bcd_byte + (l_call_dest_no[l_loop_cnt * 2 + 1] - 48) ;
break;
case 'A':
l_bcd_byte = l_bcd_byte + 10;
break;
case 'B':
l_bcd_byte = l_bcd_byte + 11;
break;
case 'C':
l_bcd_byte = l_bcd_byte + 12;
break;
case 'D':
l_bcd_byte = l_bcd_byte + 13;
break;
case 'E':
l_bcd_byte = l_bcd_byte + 14;
break;
case 'F':
case ' ':
l_bcd_byte = l_bcd_byte + 15;
break;
default:
printf("*** invalid call destination number ***\n");
return_status = FAILURE;
break;
}
}
else
l_bcd_byte = l_bcd_byte + 15;
if (l_bcd_byte == 255)
l_call_dest_no_in_bcd[l_loop_cnt] = '\0';
else
{
l_call_dest_no_in_bcd[l_loop_cnt] = l_bcd_byte;
l_call_dest_no_bcd_length++;
}
}