21

I want to convert a string, using the string class - to Binary. What is the fast way to do this character by character. Loop? Or is there some function out there that will convert for me? 1's and 0's binary.

A string being:

#include <string>
using namespace std;
int main(){
  myString = "Hello World";
}
4
  • 1
    you want this: stackoverflow.com/questions/505021/… Commented Apr 17, 2012 at 2:11
  • @Serdalis I really have no idea what's going on there. Commented Apr 17, 2012 at 2:14
  • What would you like to see as the output for "Hello, world"? 11*8 0 or 1 characters representing binary ASCII codes of the string characters, or something else? Commented Apr 17, 2012 at 2:15
  • So what have you tried - the code that you have does not even compile... Commented Apr 17, 2012 at 2:27

4 Answers 4

46

Using std::bitset would work:

#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main(){
  string myString = "Hello World";
  for (std::size_t i = 0; i < myString.size(); ++i)
  {
      cout << bitset<8>(myString.c_str()[i]) << endl;
  }
}

Output:

01001000
01100101
01101100
01101100
01101111
00100000
01010111
01101111
01110010
01101100
01100100
Sign up to request clarification or add additional context in comments.

4 Comments

Or just bitset<8>(myString[i])
so bitset<8> would give you base-256, if you want say base-255 or base-257 just add -1 and +1?
if we want to sum the outputs such that if there is an overflow bit (0 or 1) at the end of the sum on MSB side, then that should get added to the final answer of the sum again. how can we do that?
Add some context by explaining how your answer solves the problem in question, instead of posting a code-only answer.
2

Try using this with method. Example:

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

string TextToBinaryString(string words) {
    string binaryString = "";
    for (char& _char : words) {
        binaryString +=bitset<8>(_char).to_string();
    }
    return binaryString;
}
int main()
{
    string testText = "Hello World";
    cout << "Test text: " << testText << "!\n";
    cout << "Convert text to binary: " << TextToBinaryString(testText) << "!\n";

    return 0;
}

result code:

Test text: Hello World!                                                                                                                                                                                 
Convert text to binary: 0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100!

Comments

0

char * buf = data.c_str; //buf is binary

1 Comment

Add some context by explaining how your answer solve the problem in question, instead of posting code-only answer.
-1

`QString StringToBinary::toBinary(QString s) { QString bin = "";

for (int i = 0; i < s.size(); ++i)
{
    bin += QString::fromStdString(std::bitset<8>(s.at(i).unicode()).to_string());

} qDebug()<<bin;

return bin;

}`

3 Comments

I see nothing in the question to indicate the asker has any idea what a QString is. It's certainly not part of standard C++.
It's not a part of C++ but it is used in QTC++ as a String datatype
Then your answer should explain that, and also explain why it's a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.