【问题标题】:Convert strings between hex format and binary format在十六进制格式和二进制格式之间转换字符串
【发布时间】:2013-08-19 09:54:31
【问题描述】:

是否有任何实用程序或库提供了在十六进制/二进制格式之间转换字符串的简单函数?我一直在搜索 SO,目前正在使用查找表方法。顺便说一句,由于它可能是一个长字符串,我不会考虑将字符串转换为整数并处理格式转换,因为长字符串可能大于 MAX_INT(或其他整数数据类型)。

例如:

0xA1 => 10100001
11110001 => 0xF1

PS:我的项目使用的是 Boost 1.44,有点过时了。因此,如果该实用程序来自 Boost,希望它在 1.44 中可用。

【问题讨论】:

  • 什么样的“十六进制/二进制格式”?愿意举个例子吗?

标签: c++


【解决方案1】:

您可以在 C++03 中使用 std::stringstreamstd::hexstd::bitset 的组合在十六进制和二进制之间进行转换。

这是一个例子:

#include <iostream>
#include <sstream>
#include <bitset>
#include <string>

using namespace std;

int main()
{
    string s = "0xA";
    stringstream ss;
    ss << hex << s;
    unsigned n;
    ss >> n;
    bitset<32> b(n);
    // outputs "00000000000000000000000000001010"
    cout << b.to_string() << endl;
}

编辑:

关于精炼的问题,这里有一个关于在十六进制字符串和二进制字符串之间转换的代码示例(您可以使用 hex charbits 部分的辅助函数重构,并使用映射或开关等)。

const char* hex_char_to_bin(char c)
{
    // TODO handle default / error
    switch(toupper(c))
    {
        case '0': return "0000";
        case '1': return "0001";
        case '2': return "0010";
        case '3': return "0011";
        case '4': return "0100";
        case '5': return "0101";
        case '6': return "0110";
        case '7': return "0111";
        case '8': return "1000";
        case '9': return "1001";
        case 'A': return "1010";
        case 'B': return "1011";
        case 'C': return "1100";
        case 'D': return "1101";
        case 'E': return "1110";
        case 'F': return "1111";
    }
}

std::string hex_str_to_bin_str(const std::string& hex)
{
    // TODO use a loop from <algorithm> or smth
    std::string bin;
    for(unsigned i = 0; i != hex.length(); ++i)
       bin += hex_char_to_bin(hex[i]);
    return bin;
}

【讨论】:

  • 您可能需要更改声明 unsigned n;为 unsigned long long n; (是的,其中有两个长)
  • bitset 需要在编译时定义的大小。这如何与可变长度的十六进制字符串一起使用?
  • @KokHowTeh:您可以使用boost.org/doc/libs/1_63_0/libs/dynamic_bitset/… 或其他解决方案。
【解决方案2】:

以下代码包含两个完全按照您的意愿执行的函数。这是基于 Silex 的答案,但有一些额外的字符串操作来匹配您在问题中给出的示例输出。

#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
#include <boost/algorithm/string.hpp>

using namespace std;

const unsigned g_unMaxBits = 32;

string Hex2Bin(const string& s)
{
    stringstream ss;
    ss << hex << s;
    unsigned n;
    ss >> n;
    bitset<g_unMaxBits> b(n);

    unsigned x = 0;
    if (boost::starts_with(s, "0x") || boost::starts_with(s, "0X")) x = 2;
    return b.to_string().substr(32 - 4*(s.length()-x));
}

string Bin2Hex(const string& s)
{
    bitset<g_unMaxBits> bs(s);
    unsigned n = bs.to_ulong();
    stringstream ss;
    ss << hex << n;
    return "0x" + boost::to_upper_copy(ss.str());
}

int main()
{
    cout << "0xA1 => " << Hex2Bin("0xA1") << endl;
    cout << "B3 => " << Hex2Bin("B3") << endl;
    cout << "11110001 => " << Bin2Hex("11110001") << endl;

    return 0;
}

【讨论】:

    【解决方案3】:

    不使用bitset或任何库的最简单解决方案,只使用移位运算符和std::hex来转换整个十六进制字符串.

    将十六进制字符串转换为二进制数字的示例代码:

    string s = "FF 0F F0 C3 10";
    stringstream ss;
    ss << hex << s;
    
    unsigned int n;
    while(ss >> n){
       for (int i = 8 -1; i >= 0; i--) 
        cout << ((n >> i) & 1) ? "1" : "0";
     cout << std::endl;
    }
    

    样本输出:

    11111111
    00001111
    11110000
    11000011
    00001010
    

    full code snipet

    【讨论】:

      【解决方案4】:

      如果你想要一个简洁的 c++11 解决方案。给你:

      string hextobin(const string &s){
          string out;
          for(auto i: s){
              uint8_t n;
              if(i <= '9' and i >= '0')
                  n = i - '0';
              else
                  n = 10 + i - 'A';
              for(int8_t j = 3; j >= 0; --j)
                  out.push_back((n & (1<<j))? '1':'0');
          }
      
          return out;
      }
      
      string bintohex(const string &s){
          string out;
          for(uint i = 0; i < s.size(); i += 4){
              int8_t n = 0;
              for(uint j = i; j < i + 4; ++j){
                  n <<= 1;
                  if(s[j] == '1')
                      n |= 1;
              }
      
              if(n<=9)
                  out.push_back('0' + n);
              else
                  out.push_back('A' + n - 10);
          }
      
          return out;
      }
      

      这适用于任何长度的字符串,无需填充。 应该很快,因为没有长的 switch case 并且位操作很快。

      示例用法:

      string s = "FF11";
      string b = hextobin(s);
      cout << b << endl; // prints 1111111100010001
      string h = bintohex(b);
      cout << h << endl; // prints FF11
      

      【讨论】:

        【解决方案5】:
        #include <iostream>
        #include <sstream>
        #include <stdio.h>   
        #include <iostream>
        #include <string>
        
        using namespace std;
        
        char hx[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
        
        inline int hextoint(char in) {
            int const x = in;
            return (x <= 57) ? x - 48 : (x <= 70) ? (x - 65) + 0x0a : (x - 97) + 0x0a;
        }
        
        string binaryToHEX(const string& input) {
            size_t len = input.length();
            if (len % 4 != 0) {
                return "";
            }
            string output = string(len / 4, '0');
            size_t outputoffset = 0;
            for (size_t i = 0; i < len; i += 4) {
                int val = 0;
                for (size_t j = 0; j < 4; j++) {
                    if (input[i + j] == '1') {
                        val += (1 << (3 - j));
                    }
                }
                output[outputoffset++] = hx[val];
            }
            return output;
        }
        
        string HEXToBinary(const string& input) {
            size_t len = input.length();
            string output = string(len * 4, '0');
            for (size_t i = 0; i < len; i++) {
                unsigned int offset = i * 4;
                int val = hextoint(input[i]);
                for (size_t j = 0; j < 4; j++) {
                    output[offset + 3 - j] = ((val & (1 << j)) != 0) ? '1' : '0';
                }
            }
            return output;
        }
        
        int main() {
            cout << binaryToHEX("1010101101000010") << endl;
            cout << HEXToBinary("AB42") << endl;
            system("pause");
            return 1;
        }
        

        我在这里从其他人那里复制了 hextoint 函数,但我找不到帖子。

        【讨论】:

          猜你喜欢
          • 2022-07-08
          • 2012-08-15
          • 1970-01-01
          • 2011-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-19
          • 2013-05-21
          相关资源
          最近更新 更多