C++資料格式化6 - uint轉換成二六進位制字串

陌尘(MoChen)發表於2024-06-20
  • 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。

相關文章