What is the C Equivalent of Python's pack(“<

2020-03-29 02:40发布

问题:

I don't know much python but from what I can tell from the documentation the code:

   str = "AAAA"
   str += pack("<I", 0)

would append the result of the pack function to str, which would be the integer value of 0 in little-endian style. My question is what the C equivalent of this would be. Would it just be:

   char str[20] = "AAAA";
   strcat(str, "\x00");

?...

回答1:

strcat() stops at the first NUL, so no.

char str[20] = "AAAA";
int val = 0;
int nval = htole32(val);
memcpy(str + 4, (char*)&nval, 4);