一,
a = com_rx_buff[10] - 0x30;
b = com_rx_buff[11] - 0x30;
c = com_rx_buff[12] - 0x30;
m = a*100 + b*10 + c;
减0x30?
解释: a,b,c是 char 型,也可以是short型,也可以是int型的,但在这里,a的最值应该不会超过9,所以char型更合适。将接收存放在com_rx_buff[]中的数据(char)型 转换成 十进制数,
假设 com_rx_buff[11]中的数据为 '9', 在ASCII中9的十六进制是0x39,减去 ‘0‘的ASCII的十六进制0x30 就得到数字9了。
二,测试
#include<>
#include<>
#include<>
#include<>
#include<>
void main(void)
{
uint32_t val;
uint8_t distance = 138;
uint8_t tx_buf[5];
tx_buf[0] = 0x55;
tx_buf[4] = 0x0D;
tx_buf[1] = distance/100%10 + 0x30;
//printf("%d \n",tx_buf[1]);
//printf("%d \n",'0');
tx_buf[2] = distance/10%10 + 0x30;
tx_buf[3] = distance%10 + 0x30;
printf("\n %d %d %d \n",tx_buf[1],tx_buf[2],tx_buf[3]);//49 51 56
val = (tx_buf[1] - '0') * 100 + (tx_buf[2] - '0') * 10 + (tx_buf[3] - '0');
printf("val = %d \n",val);
}
执行结果:
49 51 56
val = 138