C++数据格式化6 - 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。