Convert String into Binary Sequence Last Updated : 04 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string of character the task is to convert each character of a string into the equivalent binary number. Examples : Input : GFG Output : 1000111 1000110 1000111 Input : geeks Output : 1100111 1100101 1100101 1101011 1110011 The idea is to first calculate the length of the string as n and then run a loop n times. In each iteration store ASCII value of character in variable val and then convert it into binary number and store result in array finally print the array in reverse order. Implementation: C++ // C++ program to convert // string into binary string #include <bits/stdc++.h> using namespace std; // utility function void strToBinary(string s) { int n = s.length(); for (int i = 0; i <= n; i++) { // convert each char to // ASCII value int val = int(s[i]); // Convert ASCII value to binary string bin = ""; while (val > 0) { (val % 2)? bin.push_back('1') : bin.push_back('0'); val /= 2; } reverse(bin.begin(), bin.end()); cout << bin << " "; } } // driver code int main() { string s = "geeks"; strToBinary(s); return 0; } Java // Java program to convert // string into binary string import java.util.*; class Node { // utility function static void strToBinary(String s) { int n = s.length(); for (int i = 0; i < n; i++) { // convert each char to // ASCII value int val = Integer.valueOf(s.charAt(i)); // Convert ASCII value to binary String bin = ""; while (val > 0) { if (val % 2 == 1) { bin += '1'; } else bin += '0'; val /= 2; } bin = reverse(bin); System.out.print(bin + " "); } } static String reverse(String input) { char[] a = input.toCharArray(); int l, r = 0; r = a.length - 1; for (l = 0; l < r; l++, r--) { // Swap values of l and r char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } // Driver code public static void main(String[] args) { String s = "geeks"; strToBinary(s); } } // This code is contributed by 29AjayKumar Python3 # Python 3 program to convert # string into binary string # utility function def strToBinary(s): bin_conv = [] for c in s: # convert each char to # ASCII value ascii_val = ord(c) # Convert ASCII value to binary binary_val = bin(ascii_val) bin_conv.append(binary_val[2:]) return (' '.join(bin_conv)) # Driver Code if __name__ == '__main__': s = 'geeks' print (strToBinary(s)) # This code is contributed # by Vikas Chitturi C# // C# program to convert // string into binary string using System; public class Node { // utility function static void strToBinary(String s) { int n = s.Length; for (int i = 0; i < n; i++) { // convert each char to // ASCII value int val = s[i]; // Convert ASCII value to binary String bin = ""; while (val > 0) { if (val % 2 == 1) { bin += '1'; } else bin += '0'; val /= 2; } bin = reverse(bin); Console.Write(bin + " "); } } static String reverse(String input) { char[] a = input.ToCharArray(); int l, r = 0; r = a.Length - 1; for (l = 0; l < r; l++, r--) { // Swap values of l and r char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.Join("",a); } // Driver code public static void Main(String[] args) { String s = "geeks"; strToBinary(s); } } /* This code is contributed by PrinciRaj1992 */ PHP <?php // PHP program to convert // string into binary string // utility function function strToBinary($s) { $n = strlen($s); for ($i = 0; $i < $n; $i++) { // convert each char to // ASCII value $val = ord($s[$i]); // Convert ASCII value to // binary $bin = ""; while ($val > 0) { ($val % 2)? $bin=$bin.'1' : $bin=$bin.'0'; $val= floor($val / 2); } for($x = strlen($bin) - 1; $x >= 0; $x--) echo $bin[$x]; echo " "; } } // Driver code $s = "geeks"; strToBinary($s); // This code is contributed by mits ?> JavaScript <script> // Javascript program to convert // string into binary string // utility function function strToBinary(s) { let n = s.length; for (let i = 0; i < n; i++) { // convert each char to // ASCII value let val = (s[i]).charCodeAt(0); // Convert ASCII value to binary let bin = ""; while (val > 0) { if (val % 2 == 1) { bin += '1'; } else bin += '0'; val = Math.floor(val/2); } bin = reverse(bin); document.write(bin + " "); } } function reverse(input) { a = input.split(""); let l, r = 0; r = a.length - 1; for (l = 0; l < r; l++, r--) { // Swap values of l and r let temp = a[l]; a[l] = a[r]; a[r] = temp; } return (a).join(""); } // Driver code let s = "geeks"; strToBinary(s); // This code is contributed by rag2127 </script> Output1100111 1100101 1100101 1101011 1110011 Time complexity : O(n) Auxiliary Space : O(n) Convert String into Binary Sequence Comment More infoAdvertise with us Next Article Convert all numbers in range [L, R] to binary number S sourav17031999 Follow Improve Article Tags : Misc Strings DSA binary-string Practice Tags : MiscStrings Similar Reads Convert all numbers in range [L, R] to binary number Given two positive integer numbers L and R. The task is to convert all the numbers from L to R to binary number. Examples: Input: L = 1, R = 4Output: 11011100Explanation: The binary representation of the numbers 1, 2, 3 and 4 are: 1 = (1)22 = (10)23 = (11)24 = (100)2 Input: L = 2, R = 8Output:101110 5 min read Generate all binary strings from given pattern Given a string containing of '0', '1' and '?' wildcard characters, generate all binary strings that can be formed by replacing each wildcard character by '0' or '1'. Example : Input: str = "1??0?101"Output: 1000010110001101101001011010110111000101110011011110010111101101 Recommended PracticeGenerate 13 min read Check If it is Possible to Convert Binary String into Unary String Given a binary string S of length N. You can apply following operation on S any number of times. Choose two adjacent characters, such that both are 1's or 0's. Then invert them, Formally, if both are 0's then into 1's or vice-versa. Then your task is to output YES or NO, by checking that using given 6 min read Program to Convert Octal Number to Binary Number Given an Octal number as input, the task is to convert that number to Binary number. Examples: Input : Octal = 345Output : Binary = 11100101Explanation : Equivalent binary value of 3: 011Equivalent binary value of 4: 100Equivalent binary value of 5: 101 Input : Octal = 120Output : Binary = 1010000 O 9 min read Binary representation of next number Given a binary string that represents binary representation of positive number n, the task is to find the binary representation of n+1. The binary input may or may not fit in an integer, so we need to return a string.Examples: Input: s = "10011"Output: "10100"Explanation: Here n = (19)10 = (10011)2n 6 min read Binary representation of previous number Given a binary input that represents binary representation of positive number n, find binary representation of n-1. It may be assumed that input binary number is greater than 0.The binary input may or may not fit even in unsigned long long int. Examples: Input : 10110Output : 10101Here n = (22)10 = 6 min read Like