Problems with memcpy with unsigned char array

I have a question about memcpy that I hope someone can answer. Here is a small demo program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main (int argc, char **argv){
  unsigned char buffer[10];
  unsigned short checksum = 0x1234;
  int i;
  memset(buffer, 0x00, 10);
  memcpy(buffer, (const unsigned char*)&checksum, 2);
  for(i = 0; i < 10; i ++){
    printf("%02x",buffer[i]);
  }
  printf("\n");
  return 0;
}

When I run this program, I get 34120000000000000000.
My question is: why am I not getting 12340000000000000000?

Many thanks

+3
source share
3 answers

You get 34120000000000000000 because you are on a little-endian system. You will receive 12340000000000000000 on the big-endian system. Endianness gives a full discussion of systems with large and low orders.

+8
source

little endian / big endian architecture? which means that 2 bytes of the checksum are inverted.

, . , .

+2

Intel processors are slightly oriented, they first store numbers with a small number of words.

This, apparently, indicates that Intel does not conduct testing in the domestic market.

+1
source

Source: https://habr.com/ru/post/1775788/


All Articles