Binary stream: use (char *) in C++ to pass int value

Preface

Previously participated in the small project of Logitech G29 steering wheel remote control autolabor car. Raspberry Pi controls the car as the server, and the part that collects the steering wheel data is the client. P2P communication is carried out between the two, and the steering wheel, throttle, and gear data are sent to the tree. Raspberry Pi needs to use the binary stream transfer method. This article introduces the principle of passing integer arrays through the binary stream.

One, use the binary stream to pass the integer array value

1.1 Integer array

For example, int num[3]={1, 2, 3}; there are three values ​​1, 2, 3 stored in the array num. Because int is4 bytes, The storage address interval of each value is 4 bytes.

#include<iostream>
using namespace std;
int main(int argc,char* argv[])
{
    
    
    int num[3] = {
    
    1, 2, 3};
    for (int i = 0; i < 3;++i)
        cout<<"num["<<i<<"]的地址:" 
            << &num[i] <<"  "
            <<"num["<<i<<"]的值:"
            <<num[i]<< endl;
    return 0;
}
/*---------------运行结果------------------

num[0]的地址:010FF878  num[0]的值:1
num[1]的地址:010FF87C  num[1]的值:2
num[2]的地址:010FF880  num[2]的值:3

*/

We can check how the integer decimal numbers 1, 2, 3 are stored in the memory space address, just look at the address of num[0] where 01 00 00 00 is stored, because num[0] belongs to the int type, which is4 bytes, 1 Byte=8 bit, where 01 is a byte, 8 bits. So in the int type, every time the array index is shifted, the address is shifted by 4 bytes and output.

0x010FF878  01 00 00 00
0x010FF87C  02 00 00 00
0x010FF880  03 00 00 00

1.2 Binary stream

1.2.1 How to store the integer value in the binary stream sequentially by one byte?

Introduced char *buff=(char *)&num[0], this statement is (char *)numequivalent, because the array name is equivalent to the first address of the array. num is an int type array, each value in the array num is stored in a memory address in 4 bytes, and char is a character type, which isA byte, (Char *)& means to convert num from an int pointer to a character pointer, and the address is shifted by one byte and stored in order, so that the integer number 1 can be stored in the char * in the order of 01 00 00 00 buff.

At this time, the binary stream stored in the buff is as follows:

buff[0]=01 buff[1]=00 buff[2]=00 buff[3]=00
buff[4]=02 buff[5]=00 buff[6]=00 buff[7]=00
buff[8]=03 buff[9]=00 buff[10]=00 buff[11]=00

1.2.2 How to reconstruct the integer value?

After that, the integer number is reconstructed by 4 bytes and output, and (int *)buffthe character pointer buff is converted into an integer pointer, so that each shift is 4 bytes.

Use (int*)buff, combine buff[0] to buff[3] to
use (int*)(buff+4), combine buff[4] to buff[7] to
use (int*)(buff+8), combine buff[8] to buff[11]

#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
    
    
    int num[3] = {
    
     1, 2, 3 };
    char* buff;
    buff = (char*)num;
    for (int i = 0; i < 3;++i)
    {
    
    
        cout << "num[" << i << "]的地址:"
            << &num[i] << "  "
            << "num[" << i << "]的值:"
            << num[i] << endl;

        cout << "二进制流合并重构:"
            << (int*)(buff + 4 * i) << "    值为:"
            << *((int*)(buff + 4 * i)) << endl;
    }     
    return 0;
}
/*---------------运行结果------------------

num[0]的地址:00F3FCB8  num[0]的值:1
二进制流合并重构:00F3FCB8    值为:1
num[1]的地址:00F3FCBC  num[1]的值:2
二进制流合并重构:00F3FCBC    值为:2
num[2]的地址:00F3FCC0  num[2]的值:3
二进制流合并重构:00F3FCC0    值为:3

*/

Two, structure array assignment

#include<iostream>
using namespace std;

typedef struct birthday
{
    
    
    int year;
    int month;
    int day;
} birthday;

int main(int argc,char* argv[])
{
    
    
    int person_num;
    cout << "请输入人数:";
    cin >> person_num;
    int *num = new int[3*person_num];
    cout << "请依次输入" << 3 * person_num << "个整数:" ;
    for (int i = 0; i < 3 * person_num; ++i)
        cin >> num[i];

    char *array = (char *)num;

    birthday *birth = new birthday[person_num];
    int j=0;
    for (int i=0; i < person_num ; ++i)
    {
    
    
        birth[i].year = *((int *)(array + 4*j));
        birth[i].month= *((int *)(array + 4*(j+1)));
        birth[i].day= *((int *)(array + 4*(j+2)));
        j=j + 3;
        switch(i)
        {
    
    
            case 0:
                cout << "CJX的生日:";
                break;
            case 1:
                cout << "WJ的生日:";
                break;
        }
        cout << birth[i].year << "-" 
        	<< birth[i].month << "-" 
        	<< birth[i].day << endl;
    }

    delete[] birth;
    birth = NULL;
    delete[] num;
    num = NULL;
    return 0;
}
/*---------------运行结果------------------

请输入人数:2
请依次输入6个整数:1999 10 5 1996 8 31
CJX的生日:1999-10-5
WJ的生日:1996-8-31

*/

Guess you like

Origin blog.csdn.net/qq_23023937/article/details/108466104