可以使用std::span来访问一段内存数据,并使用std::format来格式化十六进制字符串。以下是一个示例代码:

#include <iostream>
#include <span>
#include <format>

int main() {
    uint8_t data[] = {0x12, 0x34, 0x56, 0x78};
    std::span<uint8_t> data_span(data, sizeof(data));

    std::string hex_string;
    for (auto byte : data_span) {
        hex_string += std::format("{:02X}", byte);
    }
    std::cout << hex_string << std::endl;

    return 0;
}

该程序首先定义了一个包含四个字节的uint8_t数组,并使用std::span来创建一个指向该数组的视图。接下来,程序使用一个循环来遍历该std::span中的每个字节,并使用std::format将每个字节转换为十六进制字符串。最后,程序将结果打印到控制台。运行该程序,输出结果为:

12345678

这是将四个字节转换为十六进制字符串的结果。

c++20把一段内存数据用十六进制字符串打印出来,用std::span来实现

原文地址: https://www.cveoy.top/t/topic/sET 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录