C等效的Python代码struct.pack?

时间:2012-05-27 22:50:57

标签: python c

  

可能重复:
  What is the C Equivalent of Python's pack(“<I”, 0)

如何在Python中复制代码:

from struct import pack
string = pack("<I", 0x11111111)
print string
在C?根据我的理解\ x11是一个不可打印的角色所以......?

2 个答案:

答案 0 :(得分:2)

const char *string = "\x11\x11\x11\x11";
puts(string);

答案 1 :(得分:1)

char* memory = (char*)malloc(5); //4 bytes plus null
for(uint i=0;i<4;i++){
  memory[i] = 0x11; //creating a little-endian 4byte \0x11111111 
  // avoiding local endianess issues
};
memory[4] = 0; //To make it into a string
printf("%s\n", memory);
free(memory);