C++ 以string形式 打印 十六进制 代码示例

时间:2025-01-28 08:12:14
#include <iostream> using namespace std; int main(void) { unsigned char test[10]; test[0] = 0x85; test[1] = 0x3F; test[2] = 0x85; test[3] = 0x34; test[4] = 0x85; test[5] = 0x3F; test[6] = 0x85; test[7] = 0x34; test[8] = 0x85; test[9] = 0x3F; for(unsigned char n : test) cout << "0x" << std::hex << static_cast<unsigned short>(n) << " " ;//输出十六进制FF cout << endl; return 0; } /* $ g++ -o hex_test $ ./hex_test.exe 0x85 0x3f 0x85 0x34 0x85 0x3f 0x85 0x34 0x85 0x3f */