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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/30933.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

异步任务使用场景与实践

异步任务使用场景 根据同步/异步方式划分场景&#xff0c;各场景下常用的技术方案如下&#xff1a; 方式实现特点缺点同步HTTP RPC Cache etc.指标&#xff1a;RT、QPS、TPS、缓存命中率 等&#xff1b; 关注&#xff08;准&#xff09;实时数据&#xff0c;用户可交互1. 处…

javascript--类型检测 type of 和 instanceof

类型判断 1、typeof2、instanceof**instanceof 的原理** 3、constructor 1、typeof typeof在检测null、object、array、data的结果中都是object&#xff0c;所以无法用来区分这几个类型的区别。 <script>let a ["123",123,false,true,Symbol(1),new Date(),n…

[负载均衡详解]

负载均衡技术: 一、负载均衡集群技术的实现: 负载均衡技术类型:基于 4 层负载均衡技术和基于 7 层负载均衡技术 负载均衡实现方式:硬件负载均衡设备或者软件负载均衡 硬件负载均衡产品:F5 、深信服 、Radware 软件负载均衡产品: LVS(Linux Virtual Server)、 Hapr…

【HiveSQL】join关联on和where的区别及效率对比

测试环境&#xff1a;hive on spark spark版本&#xff1a;3.3.1 一、执行时机二、对结果集的影响三、效率对比1.内连接1&#xff09;on2&#xff09;where 2.外连接1&#xff09;on2&#xff09;where 四、总结PS 一、执行时机 sql连接中&#xff0c;where属于过滤条件&#…

ubuntu18.04 安装HBA

HBA是一个激光点云层级式的全局优化的程序&#xff0c;他的论文题目是&#xff1a;HBA: A Globally Consistent and Efficient Large-Scale LiDAR Mapping Module&#xff0c;对应的github地址是&#xff1a;HKU-Mars-Lab GitHub 学习本博客&#xff0c;可以学到gtsam安装&am…

E/Rajawali: Shader log: ERROR: 0:6: ‘GL_OES_standard_derivatives‘ : extension

错误信息 E/Rajawali: Shader log: ERROR: 0:6: ‘GL_OES_standard_derivatives’ : extension 定位 该错误存在rajawali渲染引擎 v1.1.9701以及以上 目前具体涉及的版本 v1.1.9701v1.2.1970 解决方案 在FragmentShader最前面加一行 #extension GL_OES_standard_derivat…

系统凭据钓鱼揭秘

背景 在进行内网横向移动时&#xff0c;通常会尝试抓取目标机器上的登录哈希和密码。但是&#xff0c;这种方法并不总是可行&#xff0c;因为有些目标机器可能没有这些信息&#xff0c;或者这些信息已经被清除或加密。因此&#xff0c;黑客们开始模拟Windows系统环境中的身份认…

智慧学习实践系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;企业管理&#xff0c;任务管理&#xff0c;公告管理&#xff0c;菜单管理&#xff0c;用户管理&#xff0c;基础数据管理 企业账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;任务…

视频融合共享平台LntonCVS视频监控安防系统运用多视频协议建设智慧园区方案

智慧园区&#xff0c;作为现代化城市发展的重要组成部分&#xff0c;不仅推动了产业的升级转型&#xff0c;也成为了智慧城市建设的核心力量。随着产业园区之间的竞争日益激烈&#xff0c;如何打造一个功能完善、智能化程度高的智慧园区&#xff0c;已经成为了业界广泛关注的焦…

蓝桥杯 经典算法题 压缩字符串

题目&#xff1a; 实现一个算法来压缩一个字符串。压缩的要求如下&#xff1a; 需要判断压缩能不能节省空间&#xff0c;仅在压缩后字符串比原字符串长度更短时进行压缩。 压缩的格式是将连续相同字符替换为字符 数字形式&#xff0c;例如 "AAABCCDDDD" 变为 &quo…

多线程(Lock锁,死锁,等待唤醒机制,阻塞队列,线程池)

Lock锁 虽然我们可以理解同步代码块和同步方法的锁对象问题但是我们并没有直接看到在哪里加上了锁&#xff0c;在哪里释放了锁为了更清晰的表达如何加锁和释放锁&#xff0c;JDK5以后提供了一个新的锁对象Lock Lock实现提供比使用synchronized方法和语句可以获得更广泛的锁定操…

数据可视化作业二:中国城市地铁数据可视化

目录 作业要求 一、绘制每个城市站点数量柱状图&#xff08;降序排列&#xff09; 1.1 每个城市站点数量统计 1.1.1 代码展示 1.1.2 统计结果展示 1.2 柱状图绘制 1.2.1 代码实现 1.2.2 绘制结果 二、绘制上海市地铁线路站点数饼状图 2.1 数据处理 2.2 代码实现 2.3…

实际二分搜索(写出函数,再用二分搜索法找左右边界 画图理解

实际二分搜索&#xff08;写出函数&#xff0c;再用二分搜索法找左右边界 看到最大值的最小化&#xff0c;左边界&#xff0c;最小化的最大值&#xff0c;右边界 画图理解 爱吃香蕉的珂珂 class Solution {public int minEatingSpeed(int[] piles, int h) {int left1,right10…

Mysql 8.3.0 安装

Mysql 8.3.0 安装地址&#xff1a;MySQL :: Download MySQL Community Server (Archived Versions) 下载链接&#xff1a;https://downloads.mysql.com/archives/get/p/23/file/mysql-8.3.0-linux-glibc2.28-x86_64.tar.xz 解压&#xff1a; tar -xvf mysql-8.3.0-linux-glib…

sql资料库

1、distinct(关键词distinct用于返回唯一不同的值)&#xff1a;查询结果中去除重复行的关键字 select distinct(university) from user_profile select distinct university from user_profile distinct是紧跟在select后面的&#xff0c;不能在其他位置&#xff0c;不然就…

【Linux】I/O多路复用模型 select、poll、epoll

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; &#x1f525;Linux系列专栏&#xff1a;Linux基础 &#x1f525; 给大家…

多模块存储器

随着计算机技术的发展&#xff0c;处理的信息量越来越多&#xff0c;对存储器的速度和容量要求也越来越高&#xff1b;而且随着CPU性能的不断提高、IO设备数量不断增加&#xff0c;导致主存的存取速度已经称为了整个计算机系统的性能瓶颈。这就要求我们必须提高主存的访问速度。…

令人震撼的人类智慧的科学领域-AI技术

AI&#xff0c;全称为人工智能&#xff08;Artificial Intelligence&#xff09;&#xff0c;是一门致力于让机器模仿人类智慧的科学领域。其核心技术涵盖了机器学习、自然语言处理、计算机视觉及专家系统等多个方面。AI旨在开发能够感知环境、进行逻辑推理、自主学习并做出决策…

使用Python进行机器学习入门指南

使用Python进行机器学习入门指南 机器学习(Machine Learning)是人工智能(Artificial Intelligence, AI)的一个重要分支,旨在通过算法和统计模型,使计算机系统能够自动从数据中学习和改进。Python作为机器学习领域的主流编程语言,提供了丰富的库和工具来实现各种机器学习…

【VMware】VMware虚拟机安装_配置_使用教程

一、准备工作 1、下载VMware软件&#xff1a;访问VMware官方网站&#xff0c;下载适合你操作系统的VMware Workstation Pro安装包。 下载地址&#xff1a;VMware Desktop Hypervisors for Windows, Linux, and Mac 2、准备操作系统镜像文件&#xff1a;根据你想要在虚拟机中安…