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

ops/2024/10/18 1:30:45/
  • 1. 关键词
  • 2. strfmt.h
  • 3. strfmt.cpp
  • 4. 测试代码
  • 5. 运行结果
  • 6. 源码地址

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


http://www.ppmy.cn/ops/52324.html

相关文章

513. 找树左下角的值

513. 找树左下角的值 原题链接&#xff1a;完成情况&#xff1a;解题思路&#xff1a;_513找树左下角的值_dfs记录高度_513找树左下角的值_bfs层次最左 参考代码&#xff1a;_513找树左下角的值_dfs记录高度OverviewClass DefinitionsMain LogicExplanation of dfs_findBottom…

鸿蒙开发系统基础能力:【@ohos.hiAppEvent (应用打点)】

应用打点 本模块提供了应用事件打点能力&#xff0c;包括对打点数据的落盘&#xff0c;以及对打点功能的管理配置。 说明&#xff1a; 本模块首批接口从API version 7开始支持。后续版本的新增接口&#xff0c;采用上角标单独标记接口的起始版本。 导入模块 import hiAppEve…

Uniapp在屏幕尺寸低于960出现样式错乱(开箱即用)

我司项目突然要做平板兼容,我在调试的时候发现当屏幕尺寸低于960px发现样式但凡是以rpx单位的全部失效&#xff0c;如果是以px为单位那么影响就比较小&#xff0c;当时解决方案是写了不少媒体查询和把单位rpx改成px&#xff0c;后面查阅文档发现大错特错宽屏适配只需一行代码即…

鸿蒙Harmony实战—通过登录Demo了解ArkTS

ArkTS是HarmonyOS优选的主力应用开发语言。ArkTS围绕应用开发在TypeScript&#xff08;简称TS&#xff09;生态基础上做了进一步扩展&#xff0c;继承了TS的所有特性&#xff0c;是TS的超集。 ArkTS在TS的基础上主要扩展了如下能力&#xff1a; 基本语法&#xff1a;ArkTS定义…

三.iOS核心动画 - 关于图层几何(frame,bounds,transform,position)

引言 关于UIView的布局有一个经常被问到的问题&#xff0c;frame和bounds有什么区别&#xff0c;同样CALayer也有frame和bounds这两个属性&#xff0c;还有一个与UIView的center对应的position属性&#xff0c;本篇博客我们就来详细的探讨一下图层中的frame和bounds到底有什么…

配置CentOS 7通过MSTSC连接远程桌面

正文共&#xff1a;777 字 14 图&#xff0c;预估阅读时间&#xff1a;1 分钟 前面我们介绍了如何通过VNC连接Ubuntu的远程桌面&#xff08;Ubuntu 18.04开启远程桌面连接&#xff09;&#xff0c;也介绍了如何使用微软的MSTSC来连接Ubuntu的远程桌面&#xff08;如何通过MSTSC…

ipfs核心是什么?

IPFS&#xff08;InterPlanetary File System&#xff09;的核心是一个分布式的文件系统&#xff0c;旨在将所有计算设备连接在同一个文件系统中。IPFS的核心技术和概念包括以下几个方面&#xff1a; 1. 内容寻址&#xff08;Content Addressing&#xff09; IPFS使用内容寻址…

代数扩张次数关系定理

【单扩域同构引理】 对于单扩张 K / F \mathbb{K/F} K/F有同构 F [ a ] ≅ F [ x ] / ⟨ f ( x ) ⟩ \mathbb{F}\lbrack a\rbrack\mathbb{\cong F}\lbrack x\rbrack/\left\langle f(x) \right\rangle F[a]≅F[x]/⟨f(x)⟩&#xff0c;其中 a ∈ K a \in \mathbb{K} a∈K为本原元…