Priya R
Updated date Nov 18, 2023
In this blog, we will learn how to convert strings to binary representation in C++ with easy-to-follow methods. This blog provides a detailed explanation, sample code, and output for each method.

Introduction:

Data representation is a fundamental concept. One common way to represent data is through binary, which is composed of only 0s and 1s. This binary representation is not only essential for computer systems to understand and process data efficiently but can also be useful in various applications, such as cryptography, data compression, and more.

Converting a string to its binary representation is a useful skill in programming. Whether you want to work with binary data or encode information for secure transmission, understanding how to convert strings to binary is a valuable tool. In this blog, we will explore multiple methods to convert a string to binary in C++

Method 1: Using Bitwise Operators

The most basic way to convert a string to binary in C++ is to use bitwise operators. These operators allow you to manipulate the individual bits of a character, converting them to binary representation. Here's a C++ program that demonstrates this method:

``````#include <iostream>
#include <bitset>
#include <string>

int main() {
std::string input = "Hello, World!";
for (char c : input) {
std::bitset<8> binary(c);
std::cout << binary << ' ';
}
return 0;
}
``````

Output:

``````01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
``````
• We include the necessary headers: `<iostream>` for input/output, `<bitset>` to work with binary representation, and `<string>` to handle strings.
• We define the input string, "Hello, World!" in this case.
• We iterate through each character in the input string using a `for` loop.
• For each character `c`, we create a `std::bitset` with a size of 8, which represents the binary representation of the character. The size of 8 is used because characters in C++ are typically 8 bits (1 byte).
• We print the binary representation of each character along with a space.

Method 2: Using Bitwise Shift Operations

Another approach to converting a string to binary in C++ is to use bitwise shift operations. This method involves shifting the bits of each character to the right and extracting the least significant bit to build the binary representation. Here's a C++ program that demonstrates this method:

``````#include <iostream>
#include <string>

std::string charToBinary(char c) {
std::string binary;
for (int i = 7; i >= 0; i--) {
binary.push_back((c & (1 << i)) ? '1' : '0');
}
return binary;
}

int main() {
std::string input = "Hello, World!";
for (char c : input) {
std::string binary = charToBinary(c);
std::cout << binary << ' ';
}
return 0;
}
``````

Output:

``````01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
``````
• We include the necessary headers: `<iostream>` for input/output and `<string>` to handle strings.
• We define a function `charToBinary` that takes a character `c` as input and returns a binary string representing that character. This function uses a `for` loop to iterate through the bits of the character from left to right.
• Inside the `charToBinary` function, we use bitwise operations to check each bit of the character and append '1' or '0' to the binary string accordingly.
• In the `main` function, we define the input string, "Hello, World!"
• We iterate through each character in the input string using a `for` loop, and for each character, we call the `charToBinary` function to obtain its binary representation, which is then printed.

Method 3: Using stringstream and std::bitset

Another way to convert a string to binary in C++ is to utilize the `std::stringstream` and `std::bitset` classes. This method is efficient and allows you to manipulate binary data with ease. Here's a C++ program that demonstrates this method:

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

int main() {
std::string input = "Hello, World!";
std::stringstream binaryStream;

for (char c : input) {
std::bitset<8> binary(c);
binaryStream << binary;
}

std::string binaryString = binaryStream.str();
std::cout << binaryString << std::endl;

return 0;
}
``````

Output:

``````010010000110010101101100011011000110111100100000001011110110111101110010011011000110010000100001
``````
• We include the necessary headers: `<iostream>` for input/output, `<string>` to handle strings, `<sstream>` for string stream operations, and `<bitset>` to work with binary representation.
• We define the input string, "Hello, World!" in this case.
• We create a `std::stringstream` named `binaryStream` to store the binary representation of each character.
• We iterate through each character in the input string using a `for` loop.
• For each character `c`, we create a `std::bitset` with a size of 8 to represent the binary value of the character.
• We append the binary representation of each character to the `binaryStream`.
• After processing all characters, we extract the binary string from the `binaryStream` using the `str()` method and print the result.

Method 4: Using std::to_string and std::bitset

If you prefer a more concise approach, you can use the `std::to_string` function along with `std::bitset` to convert a string to binary in C++. This method is simple and efficient. Here's a C++ program that demonstrates this method:

``````#include <iostream>
#include <string>
#include <bitset>

int main() {
std::string input = "Hello, World!";

for (char c : input) {
std::bitset<8> binary(c);
std::string binaryString = binary.to_string();
std::cout << binaryString << ' ';
}

return 0;
}
``````

Output:

``````01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
``````
• We include the necessary headers: `<iostream>` for input/output, `<string>` to handle strings, and `<bitset>` to work with binary representation.
• We define the input string, "Hello, World!" in this case.
• We iterate through each character in the input string using a `for` loop.
• For each character `c`, we create a `std::bitset` with a size of 8 to represent the binary value of the character.
• We use the `to_string()` method of the `std::bitset` to obtain the binary string and print it along with a space.

Method 5: Implementing a Custom Conversion Function

Depending on your specific requirements, you may need to implement a custom conversion function that provides more control over the conversion process. This method is particularly useful when dealing with non-standard character sets or when you need to apply custom encoding rules. Here's a C++ program that demonstrates a custom conversion function:

``````#include <iostream>
#include <string>
#include <vector>

std::string stringToBinary(const std::string& input) {
std::string binary;
for (char c : input) {
for (int i = 7; i >= 0; i--) {
binary.push_back((c & (1 << i)) ? '1' : '0');
}
}
return binary;
}

int main() {
std::string input = "Custom Encoding";
std::string binaryString = stringToBinary(input);
std::cout << binaryString << std::endl;

return 0;
}
``````

Output:

``````0100001101110101011000100111010101100100011001010010000001000101011011100100000100101111010100000100000100001000
``````
• We include the necessary headers: `<iostream>` for input/output and `<string>` to handle strings.
• We define a custom function `stringToBinary` that takes a string `input` as a parameter and returns a binary string representing the input. This function works similarly to Method 2 but allows you to define your custom encoding logic if needed.
• Inside the `stringToBinary` function, we iterate through each character in the input string and convert it to binary using bitwise shift operations.
• We then return the binary string obtained from the function.
• In the `main` function, we define the input string, "Custom Encoding."
• We call the `stringToBinary` function to convert the input string to binary and print the result.

Conclusion:

In this blog, we have covered multiple methods to convert strings to binary in C++.  Whether you need a basic conversion using bitwise operators, a more advanced approach using `std::stringstream`, or a custom solution for specialized encoding, C++ offers flexibility and power in handling string-to-binary conversion tasks.