Convert String into Binary Sequence | C ++ program to convert string into binary string ; utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void strToBinary ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { int val = int ( s [ i ] ) ; 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 = " geeks " ; strToBinary ( s ) ; return 0 ; }