且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UUID生成器问题

更新时间:2022-10-15 11:54:36

It appears that the %X specifier is expecting an int-sized input to be passed, while you seem to be passing it an unsigned char (isn't that what uuid[i] is?).

Try doing this and see if it helps:

for (int i = 0; i < 16; i++) {
    int temp = uuid[i];
    printf("%02X", temp);
}
printf("\n");

Update - New theory

OK, I looked at it more carefully after reading Matthew's answer.

The issue is memcpy along with the (unknown) type of uuid. You copy the uints over uuid. The first 4 bytes of uuid are the little-endian representation of data[0], as should be expected.

Now one theory that could explain the rest is that uint32 is actually 8 bytes long instead of 4, so when you copy the first 16 bytes of data onto uuid we see in order:

  • 4 LSB of data[0], little endian
  • 4 MSB of data[0] (junk)
  • 4 LSB of data[1], little endian
  • 4 MSB of data[1] (junk, for some reason all zeroes)
  • And nothing of data[2] and data[3].

So what is sizeof(uint32) equal to?