如何打印指针指向的字节的二进制内容?

Vis*_*a k 5 c malloc printf pointers

我有一个从 malloc 返回的指针。它指向分配大小的第一个字节。对于 EX:

  void* p=malloc(34);
Run Code Online (Sandbox Code Playgroud)

如何打印这 34 个字节中包含的二进制值。?

Jab*_*cky 6

你可能想要这个:

int i ;
void* p=malloc(34);

for (i = 0; i < 34; i++)
{
  unsigned char c = ((char*)p)[i] ;
  printf ("%02x ", c) ;
}
Run Code Online (Sandbox Code Playgroud)

它不以二进制(010011011)打印,而是以十六进制打印,但这可能就是您想要的。

但正如评论中所述,您将得到垃圾值。