Convert unsigned char array to hex string
时间: 2024-04-29 16:22:25 浏览: 151
c代码-char和unsigned char区别
Here's an example C++ code that converts an unsigned char array to a hex string:
```c++
#include <sstream>
#include <iomanip>
std::string to_hex_string(unsigned char* data, size_t len)
{
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < len; ++i)
ss << std::setw(2) << static_cast<unsigned int>(data[i]);
return ss.str();
}
```
You can call this function by passing the unsigned char array and its length as arguments. The function returns the hex string representation of the array. Here's an example usage:
```c++
unsigned char data[] = { 0x12, 0x34, 0x56, 0x78 };
std::string hex_str = to_hex_string(data, sizeof(data));
std::cout << hex_str << std::endl; // Output: "12345678"
```
阅读全文