Code about hexadecimal and ASCII conversion for communication between hardware layer and cloud server

illustrate:

This article mainly records the format conversion of the communication protocol between the gateway and the mobile terminal and the web terminal. Because this project uses the Alibaba Cloud IoT suite as the cloud server, and uses the Alibaba Cloud MQTT protocol for communication. Generally speaking, the hardware terminal protocol is a string in the form of hexadecimal, so the control protocol from the software side can preferably also support the direct sending of the hexadecimal string.
However, on the one hand, the WIFI module integrating the MQTT protocol (Chentian Studio ESP8266 fully compatible version at the end of December) has other strings in the serial output data, and the parsing is unstable, and no better solution has been found. Another convenience software side (IOS) cannot directly transmit the hexadecimal string. After debugging for two days, it was finally able to send it, but the one bit containing 0 during the conversion may be directly ignored. Finally decided to pass it in the form of a string, but send it in the same string as the protocol content. For example, the hexadecimal number 0x5A 0xFF is sent as a string in the form of "5AFF". Here, 0x5A is directly converted into "5A" in the form of a string.

1. Code logic

The character range of hexadecimal numbers is 0~F. For the sake of uniformity, all capitals are used here. According to the rules of decimal numbers/hexadecimal numbers/characters corresponding to the ASCII table, they can be divided into 0~9. And the two parts A~F are converted.
First of all, the '0~9' characters in the ASCII table are 48~57, and the 'A~F' characters are 65~70 respectively, so the corresponding ones are:

字符          十进制     / 差值 /        待转换的十六进制      待转换的十进制
0           48           48                 0               0
1           49           48                 1               1
...         ...          ...                    ...             ...
A           65           55                 A               10
...         ...          ...                    ...             ...
F           70           55                 F               15

As can be seen from the above table,
between 0 and 9, the character -48=corresponds to hexadecimal characters;
between A~F, the character -55=corresponds to hexadecimal characters;
according to the above algorithm, you can Implement the relevant code.

Second, the C language code

/*
*@brief:字符串转换成同形式的十六进制字符(比如字符串5A01007A,则WIFI_CTRLBUF中存的十六进制为0x5A 0x01 0x00 0x7A)
*@param:PRODUCTNAME:阿里云物联网套件中的产品名称(字符串格式)
        DEVICENAME:阿里云物联网套件中设备名称(字符串格式)
        TOPIC:阿里云物联网套件中设备不包含产品名称和设备名称的主题(字符串格式)
        DATALENGTH_OUT:转换后的十六进制字节长度
*@retur:如果字符串数据在0~9A~F范围,则满足条件转换成功,返回1;否则返回0
*/
unsigned char ASCII_TO_HEX(char *PRODUCTNAME,char *DEVICENAME,char *TOPIC,unsigned int DATALENGTH_OUT)
{
    u8 i,errflag = 1;
    unsigned int LENGTH;
    LENGTH = strlen(PRODUCTNAME) + strlen(DEVICENAME) + strlen(TOPIC) + 16;
    DATALENGTH_OUT = (DATALENGTH_OUT*2 + LENGTH);
    for(i=LENGTH;i<DATALENGTH_OUT;i+=2)                 
    {
        if(Data_RX_BUF[i] <= 'F' && Data_RX_BUF[i] >= 'A')
        {
            if(Data_RX_BUF[i+1] <= 'F' && Data_RX_BUF[i+1] >= 'A')
                WIFI_CTRLBUF[(unsigned int)(i-48)/2] = ((Data_RX_BUF[i]-55) << 4) + (Data_RX_BUF[i+1]-55);  
            else
                WIFI_CTRLBUF[(unsigned int)(i-48)/2] = ((Data_RX_BUF[i]-55) << 4) + (Data_RX_BUF[i+1]-48);
        }
        else if(Data_RX_BUF[i] <= '9' && Data_RX_BUF[i] >= '0')
        {
            if(Data_RX_BUF[i+1] <= '9' && Data_RX_BUF[i+1] >= '0')
                WIFI_CTRLBUF[(unsigned int)(i-48)/2] = ((Data_RX_BUF[i]-48) << 4) + (Data_RX_BUF[i+1]-48);
            else
                WIFI_CTRLBUF[(unsigned int)(i-48)/2] = ((Data_RX_BUF[i]-48) << 4) + (Data_RX_BUF[i+1]-55);
        }
        else 
            errflag = 0;
            break;
    }
    if(errflag)
        return 1;
    else
        return 0;
}

/*
*@brief:十六进制字符转换成同形式的字符串(比如十六进制数组0x5A 0x01 0x00 0x7A,则BUFFER_BACKUP中存的字符串为5A01007A)
*@param:NONE
*@retur:1
*/
unsigned char HEX_TO_ASCII(void)
{
    u8 i,data_H,data_L;
    for(i=0;i<34;i+=2)
    {
        data_L = BUFFER_BACK_LORAGET[i]/0xF1;   
        data_H = BUFFER_BACK_LORAGET[i] >> 4;       
        if(data_L <= 0x09 && data_L >= 0x00)
        {
            BUFFER_BACKUP[i+1] = data_L + 48;
        }
        else if(data_L <= 0x0F && data_L >= 0x0A)
        {
            BUFFER_BACKUP[i+1] = data_L + 55;
        }
        if(data_H <= 0x09 && data_H >= 0x00)
        {
            BUFFER_BACKUP[i] = data_H + 48;
        }
        else if(data_H <= 0x0F && data_H >= 0x0A)
        {
            BUFFER_BACKUP[i] = data_L + 55;
        }
    }
    return 1;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325602327&siteId=291194637