Convert String into Binary Sequence
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.
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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 */ |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output :
1100111 1100101 1100101 1101011 1110011
Recommended Posts:
- Convert an unbalanced bracket sequence to a balanced sequence
- Minimum steps to convert one binary string to other only using negation
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum swaps required to convert one binary string to another
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Minimum number of given operations required to convert a string to another string
- Convert the string into palindrome string by changing only one character.
- Convert Hexadecimal value String to ASCII value String
- Convert Binary fraction to Decimal
- String with additive sequence
- Convert Ternary Expression to a Binary Tree
- Convert decimal fraction to binary number
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Lexicographically largest sub-sequence of the given string
- Replace every consonant sequence with its length in the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



