将二进制字符串转换为ASCII字符串(C ++)

时间:2014-04-28 14:51:07

标签: c++ binary ascii

我有一个包含32位二进制的字符串变量。将二进制表示的这4个字符(8位是一个字符)转换回ASCII字符的最佳方法是什么?

例如,变量包含“01110100011001010111001101110100”,应将其转换回字符串“test”。

6 个答案:

答案 0 :(得分:7)

如果你正在使用C ++ 11,那么另一种选择:

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

int main()
{
    std::string data = "01110100011001010111001101110100";
    std::stringstream sstream(data);
    std::string output;
    while(sstream.good())
    {
        std::bitset<8> bits;
        sstream >> bits;
        char c = char(bits.to_ulong());
        output += c;
    }

    std::cout << output;

   return 0;
}

请注意,bitset是C ++ 11的一部分。

另请注意,如果数据格式不正确,当sstream.good()返回false时,结果将被静默截断。

答案 1 :(得分:0)

嗯,显然你必须遍历原始字符串,将8“位”的字符组合成一块并将它们粘在一起形成一个字符串。

// error checking is for production code
// this means that you will have to ensure that:
// input is a string of inputlen chars where inputlen is a multiple of 8
// each char is either '0' or '1'
::std::string result;
for(size_t i = 0; i < inputlen; i += 8)
{
    for(size_t j = 0; j < 8; ++j)
        c = (c << 1) | "01"[input[i + j] - '0'];
    result.push_back(c);
}

答案 2 :(得分:0)

export PATH=$PATH:/usr/lib/chromium-browser; export PATH=$PATH:/usr/lib/chromium-browser/chromedriver; . /home/michal/robot_env/bin/activate; robot -L TRACE /home/michal/project_robot/tests

输出:Hello World

Fastest way to Convert String to Binary?

要将字符串转换为二进制,我参考了上面的答案链接。

Convert a string of binary into an ASCII string (C++)

为了将二进制转换为字符串,我提到了上面的答案链接,Dale Wilson的答案。

答案 3 :(得分:0)

尝试将其与方法配合使用。示例:

#include <iostream>
#include <bitset>
#include <sstream>
using namespace std;

string BinaryStringToText(string binaryString) {
    string text = "";
    stringstream sstream(binaryString);
    while (sstream.good())
    {
        bitset<8> bits;
        sstream >> bits;
        text += char(bits.to_ulong());
    }
    return text;
}

int main()
{
    string binaryString = "0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100";
    cout << "Binary string: " << binaryString << "!\n";
    cout << "Result binary string to text: " << BinaryStringToText(binaryString) << "!\n";

    return 0;
}

结果代码:

Binary string: 0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100!                                                                                                  
Result binary string to text: Hello World! 

答案 4 :(得分:0)

这是我的尝试:

std::string str2bits(const std::string_view &str, bool big_endian = false)
{
    std::string ret;
    ret.reserve(str.size() * 8);
    for (size_t i = 0; i < str.length(); ++i)
    {
        const uint8_t ord = uint8_t(str[i]);
        for (int bitnum = (big_endian ? 0 : 7);; (big_endian ? (++bitnum) : (--bitnum)))
        {
            if ((big_endian && bitnum >= 8) || (!big_endian && bitnum < 0))
            {
                break;
            }
            if (ord & (1 << bitnum))
            {
                ret += "1";
            }
            else
            {
                ret += "0";
            }
        }
    }
    return ret;
}

str2bits(“测试”)==> 01110100011001010111001101110100

答案 5 :(得分:0)

另一种方法可能是这种,它更灵活:

#include <iostream>
#include <string>
#include <math.h>

std::string BinaryTextToASCIIText(const std::string& binaryText, const unsigned int blockSize = 8, const unsigned int separatorLength = 1)
{
    std::string text(""), block;
    unsigned int separatorBlockStartIndex = blockSize;

    for (unsigned int i = 0; i < binaryText.length(); i++)
    {
        if (i == separatorBlockStartIndex)
        {
            i += separatorLength;
            separatorBlockStartIndex += blockSize + separatorLength;
        }
        
        block += binaryText[i];

        if (block.length() == 8)
        {
            char binaryTextToASCIIChar = 0;
            for (unsigned int j = 0; j < block.length(); j++)
            {
                if (block[j] == '1')
                {
                    binaryTextToASCIIChar += pow(2, block.length() - j - 1);
                }
            }

            block.clear();
            text += binaryTextToASCIIChar;
        }
    }

    return text;
}

int main()
{
    //if you have your binary text in blocks of 32 bits and the blocks have been separated with " " (the separator can be any kind of ascii character and any length) you can use the function like this:
    std::cout << BinaryTextToASCIIText("01110100011001010111001101110100", 32, 1) << std::endl;
    //here is an example of blocks of 32 bits and the "0101111011010" (13 characters long) separator
    std::cout << BinaryTextToASCIIText("010101000110100001101001011100110101111011010001000000110100101110011001000000101111011010011000010010000001110100011001010101111011010011100110111010000100001", 32, 13);

    return 0;
}

该方法当然可以更有用..当然可以改进,但效果很好。