What is the C Equivalent of Python's pack(“<I”, 0)

笑着哭i 提交于 2019-12-19 10:15:10

问题


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);


来源:https://stackoverflow.com/questions/10771070/what-is-the-c-equivalent-of-pythons-packi-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!