首页 > 编程语言 >C++数据格式化6 - uint转换成二六进制字符串

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

时间:2024-06-20 21:54:50浏览次数:14  
标签:bin std string text 二六 value separator C++ uint

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。

标签:bin,std,string,text,二六,value,separator,C++,uint
From: https://www.cnblogs.com/luoweifu/p/18259561

相关文章

  • C++数据格式化5 - uint转换成十六进制字符串&二进制的data打印成十六进制字符串
    1.关键词2.strfmt.h3.strfmt.cpp4.测试代码5.运行结果6.源码地址1.关键词关键字:C++数据格式化字符串处理std::stringinthex跨平台应用场景:int型的数据打印成十六进制字符串二进制的data打印成十六进制字符串。2.strfmt.h#pragmaonce#include<......
  • 2024华为OD机试真题- 计算三叉搜索树的高度-(C++/Java/Python)-C卷D卷-100分
     2024华为OD机试题库-(C卷+D卷)-(JAVA、Python、C++) 题目描述定义构造三叉搜索树规则如下:每个节点都存有一个数,当插入一个新的数时,从根节点向下寻找,直到找到一个合适的空节点插入。查找的规则是:1.如果数小于节点的数减去500,则将数插入节点的左子树2.如果数大于节点的......
  • C++程序编译 错误提示和评测状态
    编译常见错误提示1.[Error]expected';'before'cout'。在cout前面,缺少一个分号。2.[Error]'arr'wasnotdeclaredinthisscope。未定义变量名arr。3.[Error]ldreturned1exitstatus。重复运行错误(上一个运行的程序,输入窗口没有关掉)。  或者main写错了。......
  • 「C++」复杂模拟【壹】
    建议开启目录食用阅读本文之前建议您先看这里,如果您已经看完了,那么就可以放心大胆的学习本文了。我认为其实本文的难度还是比较大的,今天我们题是来自山东省省选,所以建议大家谨慎阅读,如果您是专业程序员当我没说。OK,那么事不宜迟,咱们来看第一题[SDOI2010]猪国杀题目描述游戏......
  • 《C++ Primer》导学系列:第 6 章 - 函数
    6.1函数基础6.1.1基本概念函数是C++程序的基本组成单元,用于将代码组织成可以复用的模块。函数通过函数名进行调用,并且可以接受参数和返回值。函数的定义包括函数头和函数体,其中函数头描述了函数的接口,函数体包含了具体的实现代码。函数的定义函数的定义通常包括返回类型......
  • 使用 TensorRT C++ API 调用GPU加速部署 YOLOv10 实现 500FPS 推理速度——快到飞起!!
    ​NVIDIA®TensorRT™是一款用于高性能深度学习推理的SDK,包含深度学习推理优化器和运行时,可为推理应用程序提供低延迟和高吞吐量。YOLOv10是清华大学研究人员近期提出的一种实时目标检测方法,通过消除NMS、优化模型架构和引入创新模块等策略,在保持高精度的同时显著降低了......
  • 【C++】类和对象(三)构造与析构
    文章目录一、类的6个默认成员函数二、构造函数干嘛的?语法定义特性综上总结什么是默认构造函数?三、析构函数干嘛的?语法定义析构顺序一、类的6个默认成员函数如果一个类中什么成员都没有,简称为空类。空类中并不是真的什么都没有。任何类在什么都不写时,编译器会自......
  • 0基础学C++ | 第02天 | 基础知识 | sizeof关键字 | 浮点型 | 字符型 | 转义字符 | 字
    前言  该文章是在B站学习C++,同时结合自己的理解整理的笔记,视频连接:https://www.bilibili.com/video/BV1et411b73Z/?p=8&spm_id_from=333.880.my_history.page.click 1、sizeof关键字作用:利用sizeof关键字可以统计数据类型所占用的内存大小语法:sizeof(数据类型/变量)#incl......
  • c++提供的类型转换
    在C++中,提供了几种不同的类型转换方式,每种转换方式有其特定的使用场景和语义。以下是C++中常见的几种类型转换方式:静态转换(StaticCast):使用static_cast进行转换,用于基本类型之间的转换,如数值类型的转换、非const对象指针的转换等。静态转换在编译时进行,不提供运行时的检......
  • 【C++修行之道】C/C++内存管理
    目录一、C/C++内存分布 1.选择题: 2.填空题: 3.sizeof和strlen区别? 二、C语言中动态内存管理方式:malloc/calloc/realloc/free 1.malloc/calloc/realloc的区别是什么?2.这里需要free(p2)吗?三、C++内存管理方式3.1new/delete操作内置类型3.2new和delete操作......