当前位置: 首页 > news >正文

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

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

相关文章:

  • kotlin之foreach跳出循环
  • GitLab项目组相关操作(创建项目组Group、创建项目组的项目、为项目添加成员并赋予权限)
  • element-plus的Tour 漫游式引导怎么去绑定Cascader 级联选择器或者它的内容 popper
  • flink学习-容错机制
  • PyMuPDF 操作手册 - 01 从PDF中提取文本
  • el-date-picker 有效时间精确到时分秒 且给有效时间添加标记
  • Ubuntu 22.04 下 CURL(C++) 实现分块上传/下载文件源码
  • 学习笔记——交通安全分析05
  • leetcode45 跳跃游戏II
  • 使用Python进行音频处理
  • k8s学习笔记(一)
  • 【AI】消融实验ablation study
  • Zookeeper 集群节点故障剔除、切换、恢复原理
  • CFD笔记
  • 【ai】tx2-nx:搭配torch的torchvision
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 【前端学习】-粗谈选择器
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • Angular4 模板式表单用法以及验证
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • co.js - 让异步代码同步化
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • git 常用命令
  • mongo索引构建
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 复习Javascript专题(四):js中的深浅拷贝
  • 前端之React实战:创建跨平台的项目架构
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 译自由幺半群
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • ​批处理文件中的errorlevel用法
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #APPINVENTOR学习记录
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (12)目标检测_SSD基于pytorch搭建代码
  • (13)DroneCAN 适配器节点(一)
  • (第三期)书生大模型实战营——InternVL(冷笑话大师)部署微调实践
  • (三)elasticsearch 源码之启动流程分析
  • (三)SvelteKit教程:layout 文件
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (译) 函数式 JS #1:简介
  • (转)程序员疫苗:代码注入
  • (转载)OpenStack Hacker养成指南
  • .apk 成为历史!
  • .net SqlSugarHelper
  • .net 发送邮件
  • .net 获取某一天 在当月是 第几周 函数
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET中 MVC 工厂模式浅析
  • [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(十)RCE (远程代码/命令执行漏洞)相关面试题
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution