1

最初の文字列のバイナリ コードを使用して、文字列を文字列に変換する必要があります。最初の部分では、これを使用しました:文字列をバイナリに変換する最速の方法? 完全に機能しましたが、新しい文字列に書き込む方法がわかりません。

これまでに使用したコードは次のとおりです。

for (size_t i = 0; i < outputInformations.size(); ++i)
{
    cout << bitset<8>(outputInformations.c_str()[i]);
}

出力:

01110100011001010111001101110100011101010111001101100101011100100110111001100001011011010110010100001010011101000110010101110011011101000111000001100001011100110111001101110111011011110111001001100100

これを新しい文字列に書き込む方法はありますか? そのため、内部にバイナリコードを含む「binary_outputInformations」という文字列があります。

4

2 に答える 2

1

使用std::ostringstream(およびできれば C++11):

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

std::string to_binary(const std::string& input)
{
    std::ostringstream oss;
    for(auto c : input) {
        oss << std::bitset<8>(c);
    }
    return oss.str();
}

int main()
{
    std::string outputInformations("testusername\ntestpassword");
    std::string binary_outputInformations(to_binary(outputInformations));
    std::cout << binary_outputInformations << std::endl;
}

出力:

01110100011001010111001101110100011101010111001101100101011100100110111001100001011011010110010100001010011101000110010101110011011101000111000001100001011100110111001101110111011011110111001001100100
于 2013-09-21T21:44:53.970 に答える