Open In App

Convert String into Binary Sequence

Last Updated : 04 Aug, 2022
Comments
Improve
Suggest changes
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++
Java Python3 C# PHP JavaScript

Output
1100111 1100101 1100101 1101011 1110011  

Time complexity : O(n) 
Auxiliary Space : O(n)


Next Article
Article Tags :
Practice Tags :

Similar Reads