This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How many byte(s)/bits is U8 - char, U16...

This seem just dummy question.

What is a problem. I have some old project, that works on 8 bit prosecos. Now I reforming for arm7 procesor.

Ok, I find out, that in old procesor is int 16 bit type, in arm is 32 bit type, no problem. In rtl.h are definitions like U32 as unsigned int, ... and on the and U8 as unsigned char. Sizeof(U8) is 1 - 1 byte (8 bit).

Than i define my own structure for exaple:

typedef struct
{
    U8   byte_1;
    U16  bytes_2;
} MY_STRUCTURE;

sizeof(MY_STRUCTURE) is 4!!! How could be this posible?

Parents
  • "Is the external data stored with least and most significant byte of multi-byte integers placed in the same order as your processor needs? If not, then you must either byte-swap all multi-byte fields in the struct after read or before write, or you must write your own code to pack/unpack from a byte array."

    I did like you said.

    part of code:

    data->ip_1     = MK_U32_FROM8S(buf[counter], buf[counter+1], buf[counter+2], buf[counter+3]);
    
    #define MK_U32_FROM8S(high8,med_high8,med_low8,low8)     \ 
        MK_U32(MK_U16(high8,med_high8), MK_U16(med_low8, low8))
    
    #define MK_U32(high16,low16)                             \ 
        ((U32)( ((U32)(high16) << 16) | (U32)(low16) ))
    
    #define MK_U16(high8,low8)                               \ 
        ((U16)( ((U16)(high8) << 8) | (U16)(low8) ))
    
    
    

    This way I'm realy sure, that I get regular value.

    Thanks four yours explanations, I learned o lot of new and useful things.

    Peter

Reply
  • "Is the external data stored with least and most significant byte of multi-byte integers placed in the same order as your processor needs? If not, then you must either byte-swap all multi-byte fields in the struct after read or before write, or you must write your own code to pack/unpack from a byte array."

    I did like you said.

    part of code:

    data->ip_1     = MK_U32_FROM8S(buf[counter], buf[counter+1], buf[counter+2], buf[counter+3]);
    
    #define MK_U32_FROM8S(high8,med_high8,med_low8,low8)     \ 
        MK_U32(MK_U16(high8,med_high8), MK_U16(med_low8, low8))
    
    #define MK_U32(high16,low16)                             \ 
        ((U32)( ((U32)(high16) << 16) | (U32)(low16) ))
    
    #define MK_U16(high8,low8)                               \ 
        ((U16)( ((U16)(high8) << 8) | (U16)(low8) ))
    
    
    

    This way I'm realy sure, that I get regular value.

    Thanks four yours explanations, I learned o lot of new and useful things.

    Peter

Children
No data