C ++字符串为二进制code /二进制code串 [英] C++ string to binary code / binary code to string

查看:303
本文介绍了C ++字符串为二进制code /二进制code串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字符串转换成与第一串的二进制code的字符串。
在第一部分我用这个:将字符串转换为二进制的最快方法完美工作,但我不能想出一个办法来记录到一个新的字符串。

这里的code我使用至今:

 的(为size_t我= 0; I< outputInformations.size(); ++ I)
{
    COUT<<位集&所述8为氢;(outputInformations.c_str()[I]);
}

输出:

<$p$p><$c$c>01110100011001010111001101110100011101010111001101100101011100100110111001100001011011010110010100001010011101000110010101110011011101000111000001100001011100110111001101110111011011110111001001100100

有没有写入到一个新的字符串这样的方法吗?所以,我呼吁一个字符串binary_outputInformations与它里面的二进制code。


解决方案

您在找这个?

 字符串的myString =Hello World的;
  标准::字符串binary_outputInformations;
  对于(的std ::为size_t我= 0; I&LT; myString.size(); ++ I)
  {
  位集&所述8是氢; B(myString.c_str()[I]);
      binary_outputInformations + = b.to_string();
  }  性病::法院LT&;&LT; binary_outputInformations;

输出:

<$c$c>0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100

I need to convert a string into a string with the binary code of the first string. For the first part i used this: Fastest way to Convert String to Binary? Worked perfectly but i can't figure out a way to write it into a new string.

Here's the code i'm using so far:

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

Output:

01110100011001010111001101110100011101010111001101100101011100100110111001100001011011010110010100001010011101000110010101110011011101000111000001100001011100110111001101110111011011110111001001100100

Is there a way to write this into a new string? So that i have a string called "binary_outputInformations" with the binary code inside it.

解决方案

Are you looking for this ?

  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;

Output :

0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100

这篇关于C ++字符串为二进制code /二进制code串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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