Вопрос

while printing the hexadecimal value(the value which is stored in a) is printing in reverse order ,

int main()
{
    int i;
    uint8_t b[4];
    int a = 0xaabbccdd;
    uint8_t *ptr;
    ptr = &b;
    memcpy(ptr,&a,4 * sizeof(uint8_t));

    for(i = 0;i < 4;i++)
    {
        printf("%x  ",*ptr++);
    }
    return(0);

}

output

dd  cc  bb  aa

how to store that in the same order which i gave as a input(aabbccdd)

Это было полезно?

Решение

This has to do with ENDIANNESS, it's how the computers store the numerical values in memory.

To sum up, most modern processors (x86, x86-64, ARM) are little-endian (ARM is bi-endian now, you can configure it in hardware).

What this means is that the least significant bytes have the lowest address (little end first).

Instead of trying to go against this, I'd advise you to work around it if you really need to.

One thing you could do, if you really have to, is to use the network byte order, that is defined to be big-endian. Functions like htons(), htonl(), ntohs(), ntohl() could be helpful.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top