Between C ++ int data type and a byte interchangeable

    byte m_ch[12];
    byte* p= m_ch;
    byte *ptemp=m_ch; 

    int temp[3]={NULL};
    byte ss[4]={NULL};

int type conversion byte byte stream:


    int pointX[3]={123,465,789};

    for(int i=0;i<3;i++)
    {
        memcpy(p,&pointX[i],4);
        p+=4;
    }

m_ch [12] That is, after the conversion of the byte stream type byte


Transfer byte byte int type:

 

    for(int i=0;i<3;i++)
    {

        memcpy (ss, ptemp, 4); // 22 copies four bytes (int type is 4 bytes)

        //temp[i] = ss[0] & 0xFF;         //方法一  
        //temp[i] |= ((ss[1] << 8) & 0xFF00);
        //temp[i]|= ((ss[2] << 16) & 0xFF0000);
        //temp[i] |= ((ss[3] << 24) & 0xFF000000);

        Method II // 
        temp [i] = * (int *) ss; // first two methods (byte *) turn into strong pointer (int *) and then the value of pointer to int

      // temp [i] = int ( * ss); // ss error statement of type char 1 byte, expressed ss * (byte *) pointer type value, taken out of the value of the size of the size of only 1byte value, and an int is 4 bytes, therefore the statement unreasonable
        PTEMP + = 4;
    }

If wrong, let me know! Also willing to listen better advice.

 

Published 15 original articles · won praise 20 · views 10000 +

Guess you like

Origin blog.csdn.net/ZDT_zdh/article/details/82629364