Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Convert String into Binary Sequence in C++
In this tutorial, we will be discussing a program to convert string into Binary sequence.
For this we will be provided with a string of characters. Our task is to convert each of the character into its binary equivalent and print it out spaced between for different characters.
Example
#include <bits/stdc++.h>
using namespace std;
//converting into binary equivalent
void convert_binary(string s){
int n = s.length();
for (int i = 0; i <= n; i++){
//converting to ascii value
int val = int(s[i]);
//converting ascii to binary equivalent
string bin = "";
while (val > 0){
(val % 2)? bin.push_back('1') :
bin.push_back('0');
val /= 2;
}
reverse(bin.begin(), bin.end());
cout << bin << " ";
}
}
int main(){
string s = "tutorialspoint";
convert_binary(s);
return 0;
}
Output
1110100 1110101 1110100 1101111 1110010 1101001 1100001 1101100 1110011 1110000 1101111 1101001 1101110 1110100
Advertisements
