在十六进制格式和二进制格式之间转换字符串 [英] Convert strings between hex format and binary format

查看:151
本文介绍了在十六进制格式和二进制格式之间转换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如:

  0xA1 => 10100001 
11110001 => 0xF1



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

解决方案

您可以使用 std :: stringstream std :: hex std :: bitset

 <$ c 

$ c> #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)。
//输出00000000000000000000000000001010
cout<< b.to_string()<< endl;
}

编辑:



关于精确的问题,这里有一个关于十六进制字符串和二进制字符串之间转换的代码示例(您可以使用辅助函数重构十六进制char <>位部分,然后改用映射或开关等)。

  const char * hex_char_to_bin(char c)
{
// TODO句柄默认/错误
switch (toupper(c))
{
case'0':return0000;
case'1':return0001;
case'2':return0010;
case'3':return0011;
case'4':return0100;
case'5':return0101;
case'6':return0110;
case'7':return0111;
case'8':return1000;
case'9':return1001;
case'A':return1010;
case'B':return1011;
case'C':return1100;
case'D':return1101;
case'E':return1110;
case'F':return1111;
}
}

std :: string hex_str_to_bin_str(const std :: string& hex)
{
// TODO使用&算法>或者smth
std :: string bin;
for(unsigned i = 0; i!= hex.length(); ++ i)
bin + = hex_char_to_bin(hex [i]);
return bin;
}


Is there any utility or library provides a simple function to convert a string between hex/binary format? I've been searching on SO and currently using look-up table approach. By the way, since it might be a long string, I wouldn't consider to convert the string to integer and process the format conversion, as a long string might be greater than MAX_INT (or other integer data types).

For example:

0xA1 => 10100001
11110001 => 0xF1

PS: My project is using Boost 1.44, a bit out-dated. So if the utility is from Boost, hopefully it's available in 1.44.

解决方案

You can use a combination of std::stringstream, std::hex and std::bitset to convert between hex and binary in C++03.

Here's an example:

#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;
}

EDIT:

About the refined question, here's a code example about converting between hex strings and binary strings (you can refactor with a helper function for the hex char<>bits part, and use a map or a switch instead, etc).

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;
}

这篇关于在十六进制格式和二进制格式之间转换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆