C++数据格式化6 - uint转换成二六进制字符串

2024-06-21 03:12

本文主要是介绍C++数据格式化6 - uint转换成二六进制字符串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

1. 关键词

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

2. strfmt.h

#pragma once#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>namespace cutl
{/*** @brief Format uint8_t value to a binary string.** @param value the value to be formatted.* @param separator the separator between each pair of binary characters, default is comma.* @return std::string the formatted string.*/std::string to_bin(uint8_t value, char separator = ',');/*** @brief Format uint16_t value to a binary string.** @param value the value to be formatted.* @param separator the separator between each pair of binary characters, default is space.* @return std::string the formatted string.*/std::string to_bin(uint16_t value, char separator = ' ');/*** @brief Format uint32_t value to a binary string.** @param value the value to be formatted.* @param separator the separator between each pair of binary characters, default is space.* @return std::string the formatted string.*/std::string to_bin(uint32_t value, char separator = ' ');/*** @brief Format uint64_t value to a binary string.** @param value the value to be formatted.* @param separator the separator between each pair of binary characters, default is space.* @return std::string the formatted string.*/std::string to_bin(uint64_t value, char separator = ' ');
} // namespace cutl

3. strfmt.cpp

#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"namespace cutl
{std::string to_bin(uint8_t value, char separator){std::string text;std::bitset<4> v1((value >> 4) & 0xF);std::bitset<4> v2(value & 0xF);text += v1.to_string();text += separator;text += v2.to_string();return text;}std::string to_bin(uint16_t value, char separator){std::string text;text += to_bin((uint8_t)((value >> 8) & 0xFF)) + separator;text += to_bin((uint8_t)(value & 0xFF));return text;}std::string to_bin(uint32_t value, char separator){std::string text;text += to_bin((uint8_t)((value >> 24) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 16) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 8) & 0xFF)) + separator;text += to_bin((uint8_t)(value & 0xFF));return text;}std::string to_bin(uint64_t value, char separator){std::string text;text += to_bin((uint8_t)((value >> 56) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 48) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 40) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 32) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 24) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 16) & 0xFF)) + separator;text += to_bin((uint8_t)((value >> 8) & 0xFF)) + separator;text += to_bin((uint8_t)(value & 0xFF));return text;}
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strfmt.h"void TestToBin()
{PrintSubTitle("TestToBin");uint8_t a = 0x0f;std::cout << "uint8: " << cutl::to_bin(a) << std::endl;uint16_t b = 0xfc;std::cout << "uint16: " << cutl::to_bin(b) << std::endl;uint32_t c = 0x1b02aefc;std::cout << "uint32: " << cutl::to_bin(c) << std::endl;uint64_t d = 0xabcdef0123456789;std::cout << "uint64: " << cutl::to_bin(d) << std::endl;
}

5. 运行结果

---------------------------------------------TestToBin----------------------------------------------
uint8: 0000,1111
uint16: 0000,0000 1111,1100
uint32: 0001,1011 0000,0010 1010,1110 1111,1100
uint64: 1010,1011 1100,1101 1110,1111 0000,0001 0010,0011 0100,0101 0110,0111 1000,1001

6. 源码地址

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

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

这篇关于C++数据格式化6 - uint转换成二六进制字符串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1080045

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Python 常用数据类型详解之字符串、列表、字典操作方法

《Python常用数据类型详解之字符串、列表、字典操作方法》在Python中,字符串、列表和字典是最常用的数据类型,它们在数据处理、程序设计和算法实现中扮演着重要角色,接下来通过本文给大家介绍这三种... 目录一、字符串(String)(一)创建字符串(二)字符串操作1. 字符串连接2. 字符串重复3. 字

C++读写word文档(.docx)DuckX库的使用详解

《C++读写word文档(.docx)DuckX库的使用详解》DuckX是C++库,用于创建/编辑.docx文件,支持读取文档、添加段落/片段、编辑表格,解决中文乱码需更改编码方案,进阶功能含文本替换... 目录一、基本用法1. 读取文档3. 添加段落4. 添加片段3. 编辑表格二、进阶用法1. 文本替换2

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

Java 字符串操作之contains 和 substring 方法最佳实践与常见问题

《Java字符串操作之contains和substring方法最佳实践与常见问题》本文给大家详细介绍Java字符串操作之contains和substring方法最佳实践与常见问题,本文结合实例... 目录一、contains 方法详解1. 方法定义与语法2. 底层实现原理3. 使用示例4. 注意事项二、su