C++字符串到二进制代码/二进制代码到字符串
C++ string to binary code / binary code to string
我需要使用第一个字符串的二进制代码将字符串转换为字符串。对于第一部分,我使用了这个:将字符串转换为二进制的最快方法?工作得很好,但我想不出把它写成新字符串的方法。
这是我到目前为止使用的代码:
for (size_t i = 0; i < outputInformations.size(); ++i)
{
cout << bitset<8>(outputInformations.c_str()[i]);
}
输出:
01110100011001010111001101110100011101010111001101100101011100100110111001100001011011010110010100001010011101000110010101110011011101000111000001100001011100110111001101110111011011110111001001100100
有没有办法将其写入新字符串?所以我有一个名为"binary_outputInformations"的字符串,里面有二进制代码。
你在找这个吗?
string myString = "Hello World";
std::string binary_outputInformations;
for (std::size_t i = 0; i < myString.size(); ++i)
{
bitset<8> b(myString.c_str()[i]);
binary_outputInformations+= b.to_string();
}
std::cout<<binary_outputInformations;
输出:
0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100
使用 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("testusernamentestpassword");
std::string binary_outputInformations(to_binary(outputInformations));
std::cout << binary_outputInformations << std::endl;
}
输出:
01110100011001010111001101110100011101010111001101100101011100100110111001100001011011010110010100001010011101000110010101110011011101000111000001100001011100110111001101110111011011110111001001100100
相关文章:
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- 编译多个C++文件.调用二进制文件以运行代码
- 如何在使用 LLVM 编译时获取部分源代码的二进制代码
- 如何将二进制文件头示例代码 (C++/C#) 转换为 Python?
- 将fwrite转换为C++类型代码以写入二进制文件
- 编译代码时如何不制作二进制文件?
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- 如何存储霍夫曼转换后的二进制代码?
- 编译器C++如何在二进制代码中表示整数
- C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
- 从二进制文件加载列表后,我的代码似乎进入了无限循环
- 在计算机级别,应用程序以二进制方式运行,然后当发生运行时错误时,它如何呈现回错误代码
- 严重性代码说明项目文件行抑制状态错误 C2676 二进制">>":"std::ifstream"
- 为什么此基准代码用于线性和二进制搜索不起作用
- 将二进制号转换为十进制数字的代码
- 在定义C2679和C2678错误代码的地方未找到二进制操作员
- 以下对二进制转换代码的小数如何工作
- 二进制搜索 - 代码编译并运行后不显示输出
- C++字符串到二进制代码/二进制代码到字符串