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

相关文章

批量导入txt数据到的redis过程

《批量导入txt数据到的redis过程》用户通过将Redis命令逐行写入txt文件,利用管道模式运行客户端,成功执行批量删除以Product*匹配的Key操作,提高了数据清理效率... 目录批量导入txt数据到Redisjs把redis命令按一条 一行写到txt中管道命令运行redis客户端成功了批量删除k

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C# $字符串插值的使用

《C#$字符串插值的使用》本文介绍了C#中的字符串插值功能,详细介绍了使用$符号的实现方式,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录$ 字符使用方式创建内插字符串包含不同的数据类型控制内插表达式的格式控制内插表达式的对齐方式内插表达式中使用转义序列内插表达式中使用

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么