C++数据格式化5 - uint转换成十六进制字符串&二进制的data打印成十六进制字符串

  • [1. 关键词](#1. 关键词)
  • [2. strfmt.h](#2. strfmt.h)
  • [3. strfmt.cpp](#3. strfmt.cpp)
  • [4. 测试代码](#4. 测试代码)
  • [5. 运行结果](#5. 运行结果)
  • [6. 源码地址](#6. 源码地址)

1. 关键词

关键字:

C++ 数据格式化 字符串处理 std::string int hex 跨平台

应用场景:

  • int 型的数据打印成十六进制字符串
  • 二进制的data打印成十六进制字符串。

2. strfmt.h

c++ 复制代码
#pragma once

#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>

namespace cutl
{
    /**
     * @brief Format data to a hex string.
     *
     * @param data the data to be formatted.
     * @param len the length of the data.
     * @param upper whether to use upper case or lower case for hex characters, default is upper case.
     * @param separator the separator between each pair of hex characters, default is space.
     * @return std::string the formatted string.
     */
    std::string to_hex(const uint8_t *data, size_t len, bool upper = true, char separator = ' ');
    /**
     * @brief Format a uint8_t value to a hex string.
     *
     * @param value the value to be formatted.
     * @param upper whether to use upper case or lower case for hex characters, default is upper case.
     * @param prefix the prefix of the formatted string, default is empty.
     * @return std::string the formatted string.
     */
    std::string to_hex(uint8_t value, bool upper = true, const std::string &prefix = "");
    /**
     * @brief Format a uint16_t value to a hex string.
     *
     * @param value the value to be formatted.
     * @param upper whether to use upper case or lower case for hex characters, default is upper case.
     * @param prefix the prefix of the formatted string, default is empty.
     * @return std::string the formatted string.
     */
    std::string to_hex(uint16_t value, bool upper = true, const std::string &prefix = "");
    /**
     * @brief Format a uint32_t value to a hex string.
     *
     * @param value the value to be formatted.
     * @param upper whether to use upper case or lower case for hex characters, default is upper case.
     * @param prefix the prefix of the formatted string, default is empty.
     * @return std::string the formatted string.
     */
    std::string to_hex(uint32_t value, bool upper = true, const std::string &prefix = "");
    /**
     * @brief Format a uint64_t value to a hex string.
     *
     * @param value the value to be formatted.
     * @param upper whether to use upper case or lower case for hex characters, default is upper case.
     * @param prefix the prefix of the formatted string, default is empty.
     * @return std::string the formatted string.
     */
    std::string to_hex(uint64_t value, bool upper = true, const std::string &prefix = "");
} // namespace cutl

3. strfmt.cpp

c++ 复制代码
#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"

namespace cutl
{
    static const char HEX_CHARS_UPPER[] = "0123456789ABCDEF";
    static const char HEX_CHARS_LOWER[] = "0123456789abcdef";

    std::string to_hex(const uint8_t *data, size_t len, bool upper, char separator)
    {
        const char *hex_chars = upper ? HEX_CHARS_UPPER : HEX_CHARS_LOWER;

        std::string output;
        output.reserve(3 * len);
        for (size_t i = 0; i < len; i++)
        {
            const char temp = data[i];
            output.push_back(hex_chars[temp / 16]);
            output.push_back(hex_chars[temp % 16]);
            output.push_back(separator);
        }

        return output;
    }

    std::string to_hex(uint8_t value, bool upper, const std::string &prefix)
    {
        const char *hex_chars = upper ? HEX_CHARS_UPPER : HEX_CHARS_LOWER;
        std::string text = prefix;
        int c1 = value / 16;
        int c2 = value % 16;
        text.push_back(hex_chars[c1]);
        text.push_back(hex_chars[c2]);
        return text;
    }
    std::string to_hex(uint16_t value, bool upper, const std::string &prefix)
    {
        std::string text = prefix;
        text += to_hex((uint8_t)((value >> 8) & 0xFF), upper);
        text += to_hex((uint8_t)(value & 0xFF), upper);
        return text;
    }

    std::string to_hex(uint32_t value, bool upper, const std::string &prefix)
    {
        std::string text = prefix;
        text += to_hex((uint8_t)((value >> 24) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 16) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 8) & 0xFF), upper);
        text += to_hex((uint8_t)(value & 0xFF), upper);
        return text;
    }

    std::string to_hex(uint64_t value, bool upper, const std::string &prefix)
    {
        std::string text = prefix;
        text += to_hex((uint8_t)((value >> 56) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 48) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 40) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 32) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 24) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 16) & 0xFF), upper);
        text += to_hex((uint8_t)((value >> 8) & 0xFF), upper);
        text += to_hex((uint8_t)(value & 0xFF), upper);
        return text;
    }
} // namespace cutl

4. 测试代码

c++ 复制代码
#include "common.hpp"
#include "strfmt.h"

void TestToHex()
{
    PrintSubTitle("TestToHex");

    uint8_t a = 0x0f;
    std::cout << "uint8: " << cutl::to_hex(a) << std::endl;
    uint16_t b = 0xfc;
    std::cout << "uint16: " << cutl::to_hex(b) << std::endl;
    uint32_t c = 0x1b02aefc;
    std::cout << "uint32: " << cutl::to_hex(c) << std::endl;
    uint64_t d = 0xabcdef0123456789;
    std::cout << "uint64: " << cutl::to_hex(d) << std::endl;
    uint8_t bytes[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10};
    std::cout << "bytes: " << cutl::to_hex(bytes, 16) << std::endl;
}

5. 运行结果

bash 复制代码
---------------------------------------------TestToHex----------------------------------------------
uint8: 0F
uint16: 00FC
uint32: 1B02AEFC
uint64: ABCDEF0123456789
bytes: 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

本文由博客一文多发平台 OpenWrite 发布!

相关推荐
van叶~37 分钟前
算法妙妙屋-------1.递归的深邃回响:二叉树的奇妙剪枝
c++·算法
knighthood20011 小时前
解决:ros进行gazebo仿真,rviz没有显示传感器数据
c++·ubuntu·ros
半盏茶香1 小时前
【C语言】分支和循环详解(下)猜数字游戏
c语言·开发语言·c++·算法·游戏
小堇不是码农1 小时前
在VScode中配置C_C++环境
c语言·c++·vscode
Jack黄从零学c++1 小时前
C++ 的异常处理详解
c++·经验分享
捕鲸叉7 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer7 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq7 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
青花瓷9 小时前
C++__XCode工程中Debug版本库向Release版本库的切换
c++·xcode
幺零九零零10 小时前
【C++】socket套接字编程
linux·服务器·网络·c++