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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class Node
{
static void strToBinary(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
int val = Integer.valueOf(s.charAt(i));
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--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
public static void main(String[] args)
{
String s = "geeks" ;
strToBinary(s);
}
}
|
Python3
def strToBinary(s):
bin_conv = []
for c in s:
ascii_val = ord (c)
binary_val = bin (ascii_val)
bin_conv.append(binary_val[ 2 :])
return ( ' ' .join(bin_conv))
if __name__ = = '__main__' :
s = 'geeks'
print (strToBinary(s))
|
C#
using System;
public class Node
{
static void strToBinary(String s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
int val = s[i];
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--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
public static void Main(String[] args)
{
String s = "geeks" ;
strToBinary(s);
}
}
|
PHP
<?php
function strToBinary( $s )
{
$n = strlen ( $s );
for ( $i = 0; $i < $n ; $i ++)
{
$val = ord( $s [ $i ]);
$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 " " ;
}
}
$s = "geeks" ;
strToBinary( $s );
?>
|
Javascript
<script>
function strToBinary(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
let val = (s[i]).charCodeAt(0);
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--)
{
let temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return (a).join( "" );
}
let s = "geeks" ;
strToBinary(s);
</script>
|
Output
1100111 1100101 1100101 1101011 1110011
Time complexity : O(n)
Auxiliary Space : O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Aug, 2022
Like Article
Save Article