Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

1. 关键词

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

2. strfmt.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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. 测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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. 运行结果

1
2
3
4
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。

推荐阅读
C++数据格式化5 - uint转换成十六进制字符串&二进制的data打印成十六进制字符串 C++数据格式化5 - uint转换成十六进制字符串&二进制的data打印成十六进制字符串 C++系统相关操作7 - 判断系统大小端&大小端的数据转换 C++系统相关操作7 - 判断系统大小端&大小端的数据转换 C++数据格式化1 - uint转换成字符串 & double转换成字符串 C++数据格式化1 - uint转换成字符串 & double转换成字符串

评论