首页 > 编程语言 >C++数据格式化5 - uint转换成十六进制字符串&二进制的data打印成十六进制字符串

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

时间:2024-06-21 10:01:51浏览次数:11  
标签:std 十六进制 string upper text hex value 字符串 data

1. 关键词

关键字:

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

应用场景:

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

2. strfmt.h

#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

#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. 测试代码

#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. 运行结果

---------------------------------------------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 发布!

标签:std,十六进制,string,upper,text,hex,value,字符串,data
From: https://blog.csdn.net/luoweifu/article/details/139843369

相关文章

  • 请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言提供的求字符串长度
    /*请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言提供的求字符串长度的函数)函数返回较长的字符串。若两个字符串长度相同,则返回第一个字符串。*/#include<stdio.h>char*fun(char*buff,char*str){intbuff_len=0,str_len=0;while(bu......
  • [翻译]-Detect And Repair Corruption in an Oracle Database
    本文是对这篇文章DetectAndRepairCorruptioninanOracleDatabase[1]的翻译,翻译如有不当的地方,敬请谅解,请尊重原创和翻译劳动成果,转载的时候请注明出处。谢谢!Oracle数据库提供了多种方法检测和修复数据文件中的坏块。主要有下面一些方法:RMAN(BACKUPVALIDATE,RESTOREVA......
  • 鸿蒙开发组件:【DataAbility权限控制】
    DataAbility权限控制DataAbility提供数据服务,并不是所有的Ability都有权限读写它,DataAbility有一套权限控制机制来保证数据安全。分为静态权限控制和动态权限控制两部分。静态权限控制DataAbility作为服务端,在被拉起的时候,会根据config.json里面配置的权限来进行校验,有"r......
  • 鸿蒙ArkTS声明式组件:【DataPanel】
    DataPanel数据面板组件,用于将多个数据占比情况使用占比图进行展示。说明:该组件从APIVersion7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。子组件无接口DataPanel(options:{values:number[],max?:number,type?:DataPanelType})从......
  • 强大的多数据库客户端工具:DataGrip【送源码】
    今天给大家带来的工具是:DataGrip介绍DataGrip是jetbrains开发的一款关系数据库和NoSQL数据库的多数据库客户端工具,可以30天免费试用,后续使用需要购买。DataGrip还是一款强大的跨平台工具,支持多种操作系统,比如Windows、macOS、Linux等。特性配合智能查询控制台,提供强......
  • C++数据格式化6 - uint转换成二六进制字符串
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词C++数据格式化字符串处理std::stringintbin跨平台2.strfmt.h#pragmaonce#include<string>#include<cstdint>#include<sstream>#include<iomanip>namespacecutl{......
  • C++数据格式化5 - uint转换成十六进制字符串&二进制的data打印成十六进制字符串
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词关键字:C++数据格式化字符串处理std::stringinthex跨平台应用场景:int型的数据打印成十六进制字符串二进制的data打印成十六进制字符串。2.strfmt.h#pragmaonce#include<......
  • 函数内部返回指向字符串的指针和数组名的区别
    目录两道题目进程的内存分布结论两道题目先来看两道与内存管理有关的题目以下程序会出错吗?如果不会则输出什么?#include<stdio.h>char*func(){ char*str="HelloWorld"; returnstr;}intmain(){ char*str=func(); //程序输出HelloWorld printf("%s\n",......
  • DataOps真能“降本增效”?
    在各行各业中,越来越多的公司开始重视收集数据,并寻找创新方法来获得真实可行的商业成果,并且愿意投入大量时间和金钱来实现这一目标。据IDC称,数据和分析软件及云服务市场规模在2021年达到了900亿美元,随着企业继续对人工智能和机器学习(AI/ML)和现代数据计划进行投资,预计到2......
  • DEMO_02:随机数获取;数组集合遍历;整型与字符串转换;字符串字符遍历;数组/集合排序
    /***考核点:随机数获取;数组集合遍历;整型与字符串转换;字符串字符遍历;数组/集合排序*<p>*题目:*1.使用while循环获取20个五位数随机数并打印;*2.遍历20个数,筛选出随机数中3的倍数,并统计个数;*3.符合2的数中,找出五位数中3的倍数和位置*4.符合2的数中,把这五位数......