如何以十六进制打印字节数组 - C++

问题描述

我这里有一段代码

#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
  
  
  unsigned char bytes[] = {0x43,0x4d,0x30,0x0f,0x0D};
  
  std::cout << std::hex << bytes[0] <<std::endl;
  

}

上面的程序正在将 C 打印到命令行。 如何让程序将 43 打印到命令行。 我的操作系统是 Windows 10,64 位。

解决方法

使用printf函数:

#include<cstdio>
...
printf("%x",bytes[0]);

如果您想了解有关 printf 函数的更多信息,请参阅 here