Conversion of characters and representative data

Purpose

In the interaction with the device, most of them interact with hexadecimal numbers.
When displayed to the user, it is displayed in the form of characters.
This requires the conversion of characters and the values ​​they represent, for example:
'0F'---->0x0F
How to achieve this, one is a character, the other is a number, the things stored in the computer are different, completely Two types of things.
Characters are saved in the computer through ASCII code, that is:
because the computer was invented by Americans, Americans have formulated a code table corresponding to characters and binary codes for English characters. This code table is ASCII code, which is the American Standard Information Exchange Code (American Standard Code for Information Interchange).
The specific correspondence between ASCII character codes is as follows:
insert image description here

Its essence is also numbers, and they are all arranged in order, so it is simple, start to realize:

Implementation process

int MainWindow::char2bits(char ch)
{
    
    
    int bits = 0;
    if (ch >= 'a' && ch <= 'z')
    {
    
    
        bits = ch - 'a' + 10;
    }
    else if (ch >= 'A' && ch <= 'Z')
    {
    
    
        bits = ch - 'A' + 10;
    }
    else if (ch >= '0' && ch <= '9')
    {
    
    
        bits = ch - '0';
    }
    else
    {
    
    
        bits = -1;
    }
    return bits;
}
int MainWindow::hex2bytes(const char *hex, char *bytes, int size)
{
    
    
    int len = strlen(hex);
    int nbytes = (len + 1) / 3;
    if (nbytes > size)
    {
    
    
        return -1;
    }
    int n;
    for (n = 0; n != nbytes; ++ n)
    {
    
    
        int lndx = n * 3;
        int rndx = lndx + 1;
        int lbits = char2bits(hex[lndx]);
        int rbits = char2bits(hex[rndx]);
        if (lbits == -1 || rbits == -1)
        {
    
    
            return -1;
        }
        bytes[n] = (lbits << 4) | rbits;
    }
    return nbytes;
}
void MainWindow::on_btnTest_clicked()
{
    
    
    const char *hex = "07 0A 02 10 03 00 00 00 00 00";
       char stream[10];
       int nbytes = hex2bytes(hex, stream, 10);
       if (nbytes != -1)
       {
    
    
           int i = 0;
           for ( ; i < nbytes; ++ i)
           {
    
    
               qDebug("%02x ", stream[i]);
           }
       }
}

Operation status:
insert image description here

Summarize

When I first thought about it, it felt a bit complicated, but it became simple once I knew its essence. What is the essence, characters are numbers in the computer, and this corresponding relationship is the ASCII table.
For example, the value corresponding to the character 'B' should be 11, then: 'B'-'A'=1, then add 10, isn't it 11?
Another example is '1', the corresponding data should be 1, then '1'-'0'=1
all characters are two-byte values, and they come in order. It is very simple to understand this essence up.
Finally, summarize with a picture:
insert image description here

Guess you like

Origin blog.csdn.net/maokexu123/article/details/131457987