Program to convert given Binary to its equivalent ASCII character string Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Given a binary string str, the task is to find its equivalent ASCII (American Standard Code for Information Interchange) character string. Examples: Input: str = "0110000101100010"Output: abExplanation: Dividing str into set of 8 bits as follows: 01100001 = 97, ASCII value of 97 is 'a'.01100010 = 98, ASCII value of 98 is 'b'.Therefore, the required ASCII character string is "ab". Input: str = "10000101100"Output: Not PossibleExplanation: The given binary string is not a valid string as the number of characters is not a multiple of 8. Approach: This problem is implementation-based problem. Follow the steps below to solve the given problem. At first, check if s is divisible by 8 or notIf not divisible by 8 print "Not Possible"Otherwise, move to the next stepDeclare an empty string to store all the ASCII character string.Traverses in a jump of 8 characters and in each step find the decimal equivalent value of the current set of 8 bits.Convert the decimal value to its equivalent ASCII character and append it to the res string.Return the res string.Below is the implementation of the above approach: C++14 // C++ implementation for above approach #include <bits/stdc++.h> using namespace std; // Function to convert binary to decimal int binaryToDecimal(string n) { string num = n; // Stores the decimal value int dec_value = 0; // Initializing base value to 1 int base = 1; int len = num.length(); for (int i = len - 1; i >= 0; i--) { // If the current bit is 1 if (num[i] == '1') dec_value += base; base = base * 2; } // Return answer return dec_value; } // Function to convert binary to ASCII string setStringtoASCII(string str) { // To store size of s int N = int(str.size()); // If given string is not a // valid string if (N % 8 != 0) { return "Not Possible!"; } // To store final answer string res = ""; // Loop to iterate through string for (int i = 0; i < N; i += 8) { int decimal_value = binaryToDecimal((str.substr(i, 8))); // Apprend the ASCII character // equivalent to current value res += char(decimal_value); } // Return Answer return res; } // Driver Code int main() { string s = "0110000101100010"; cout << setStringtoASCII(s); return 0; } Java // Java implementation for above approach import java.util.*; class GFG { // Function to convert binary to decimal static int binaryToDecimal(String n) { String num = n; // Stores the decimal value int dec_value = 0; // Initializing base value to 1 int base = 1; int len = num.length(); for (int i = len - 1; i >= 0; i--) { // If the current bit is 1 if (num.charAt(i) == '1') dec_value += base; base = base * 2; } // Return answer return dec_value; } // Function to convert binary to ASCII static String setStringtoASCII(String str) { // To store size of s int N = (str.length()); // If given String is not a // valid String if (N % 8 != 0) { return "Not Possible!"; } // To store final answer String res = ""; // Loop to iterate through String for (int i = 0; i < N; i += 8) { int decimal_value = binaryToDecimal((str.substring(i, 8+i))); // Apprend the ASCII character // equivalent to current value res += (char)(decimal_value); } // Return Answer return res; } // Driver Code public static void main(String[] args) { String s = "0110000101100010"; System.out.print(setStringtoASCII(s)); } } // This code is contributed by 29AjayKumar Python3 # python implementation for above approach # Function to convert binary to decimal def binaryToDecimal(n): num = n # Stores the decimal value dec_value = 0 # Initializing base value to 1 base = 1 le = len(num) for i in range(le - 1, -1, -1): # If the current bit is 1 if (num[i] == '1'): dec_value += base base = base * 2 # Return answer return dec_value # Function to convert binary to ASCII def setStringtoASCII(str): # To store size of s N = int(len(str)) # If given string is not a # valid string if (N % 8 != 0): return "Not Possible!" # To store final answer res = "" # Loop to iterate through string for i in range(0, N, 8): decimal_value = binaryToDecimal(str[i: i + 8]) # Apprend the ASCII character # equivalent to current value res += chr(decimal_value) # Return Answer return res # Driver Code if __name__ == "__main__": s = "0110000101100010" print(setStringtoASCII(s)) # This code is contributed by rakeshsahni C# // C# implementation for above approach using System; class GFG { // Function to convert binary to decimal static int binaryToDecimal(string n) { string num = n; // Stores the decimal value int dec_value = 0; // Initializing base value to 1 int base1 = 1; int len = num.Length; for (int i = len - 1; i >= 0; i--) { // If the current bit is 1 if (num[i] == '1') dec_value += base1; base1 = base1 * 2; } // Return answer return dec_value; } // Function to convert binary to ASCII static string setStringtoASCII(string str) { // To store size of s int N = (str.Length); // If given String is not a // valid String if (N % 8 != 0) { return "Not Possible!"; } // To store final answer string res = ""; // Loop to iterate through String for (int i = 0; i < N; i += 8) { int decimal_value = binaryToDecimal((str.Substring(i, 8))); // Apprend the ASCII character // equivalent to current value res += (char)(decimal_value); } // Return Answer return res; } // Driver Code public static void Main(string[] args) { string s = "0110000101100010"; Console.WriteLine(setStringtoASCII(s)); } } // This code is contributed by ukasp. JavaScript <script> // JavaScript implementation for above approach // Function to convert binary to decimal function binaryToDecimal(n) { let num = n; // Stores the decimal value let dec_value = 0; // Initializing base value to 1 let base = 1; let len = n.length; for(let i = len - 1; i >= 0; i--) { // If the current bit is 1 if (n[i] == '1') dec_value += base; base = base * 2; } // Return answer return dec_value; } // Function to convert binary to ASCII function setStringtoASCII(str) { // To store size of s let N = str.length; // If given string is not a // valid string if (N % 8 != 0) { return "Not Possible!"; } // To store final answer let res = ""; // Loop to iterate through string for(let i = 0; i < N; i = i + 8) { let decimal_value = binaryToDecimal( (str.slice(i, i + 8))); // Apprend the ASCII character // equivalent to current value res = res + String.fromCharCode(decimal_value); } // Return Answer return res; } // Driver Code let s = "0110000101100010"; document.write(setStringtoASCII(s)); // This code is contributed by Potta Lokesh </script> Outputab Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time.Auxiliary Space: O(N) Approach 2: bit shifting:In this approach, we loop through the binary string in chunks of 8 characters. For each chunk, we create an integer variable asciiValue and loop through the 8 characters in the chunk. For each character, we shift asciiValue one bit to the left and add the value of the character ('0' or '1') to the rightmost bit using the bitwise OR operator. Once we've processed all 8 characters in the chunk, we convert asciiValue to a character using a cast and append it to asciiString. Finally, we print asciiString to the console. Note that we're using static_cast<char> to convert the integer asciiValue to a character. This is safer than using a C-style cast or a regular cast because it performs compile-time checks and produces a compile error if the conversion is not allowed. here is the code of above approach: C++ #include <iostream> #include <string> using namespace std; int main() { string binaryString = "0110000101100010"; string asciiString = ""; for (size_t i = 0; i < binaryString.size(); i += 8) { int asciiValue = 0; for (size_t j = 0; j < 8; j++) { asciiValue <<= 1; asciiValue |= (binaryString[i + j] - '0'); } asciiString += static_cast<char>(asciiValue); } cout << asciiString << endl; return 0; } Java import java.util.*; public class Main { public static void main(String[] args) { String binaryString = "0110000101100010"; String asciiString = ""; for (int i = 0; i < binaryString.length(); i += 8) { int asciiValue = 0; for (int j = 0; j < 8; j++) { asciiValue <<= 1; asciiValue |= (binaryString.charAt(i + j) - '0'); } asciiString += (char)asciiValue; } System.out.println(asciiString); } } Python3 binary_string = "0110000101100010" ascii_string = "" for i in range(0, len(binary_string), 8): ascii_value = 0 for j in range(8): ascii_value <<= 1 ascii_value |= int(binary_string[i+j]) ascii_string += chr(ascii_value) print(ascii_string) C# using System; class Program { static void Main(string[] args) { // Initialize a binary string string binaryString = "0110000101100010"; // Initialize an empty string to store the ASCII // characters string asciiString = ""; // Loop through the binary string in chunks of 8 // bits (1 byte) for (int i = 0; i < binaryString.Length; i += 8) { // Initialize an integer to store the ASCII // value of each byte int asciiValue = 0; // Loop through each bit in the byte and // calculate its ASCII value for (int j = 0; j < 8; j++) { // Shift the bits of asciiValue to the left // by one position asciiValue <<= 1; // OR the last bit of asciiValue with the // current bit of binaryString asciiValue |= (binaryString[i + j] - '0'); } // Convert the ASCII value to its corresponding // character and add it to asciiString asciiString += Convert.ToChar(asciiValue); } // Print out the resulting ASCII string Console.WriteLine(asciiString); } } JavaScript const binaryString = "0110000101100010"; let asciiString = ""; for (let i = 0; i < binaryString.length; i += 8) { let asciiValue = 0; for (let j = 0; j < 8; j++) { asciiValue <<= 1; asciiValue |= parseInt(binaryString[i+j], 10); } asciiString += String.fromCharCode(asciiValue); } console.log(asciiString); Output abTime Complexity: O(N), where n is the number of elements in the input array.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Program to Convert ASCII to Unicode B bhukyavasanthkumar Follow Improve Article Tags : Strings Bit Magic Searching School Programming DSA binary-string ASCII +3 More Practice Tags : Bit MagicSearchingStrings Similar Reads What is ASCII - A Complete Guide to Generating ASCII Code The American Standard Code for Information Interchange, or ASCII, is a character encoding standard that has been a foundational element in computing for decades. It plays a crucial role in representing text and control characters in digital form. Historical BackgroundASCII has a rich history, dating 13 min read ASCII Values Alphabets ( A-Z, a-z & Special Character Table ) ASCII (American Standard Code for Information Interchange) is a standard character encoding used in telecommunication. The ASCII pronounced 'ask-ee', is strictly a seven-bit code based on the English alphabet. ASCII codes are used to represent alphanumeric data. The code was first published as a sta 7 min read ASCII Vs UNICODE Overview :Unicode and ASCII are the most popular character encoding standards that are currently being used all over the world. Unicode is the universal character encoding used to process, store and facilitate the interchange of text data in any language while ASCII is used for the representation of 3 min read ASCII Value of a Character in C In this article, we will discuss about the ASCII values that are bit numbers used to represent the character in the C programming language. We will also discuss why the ASCII values are needed and how to find the ASCII value of a given character in a C program.Table of ContentWhat is ASCII Value of 4 min read ascii() in Python Python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes. It's a built-in function that takes one argument and returns a string that represents the object using only ASCII characters. Exa 3 min read Program to Print ASCII ConversionProgram to print ASCII Value of all digits of a given numberGiven an integer N, the task is to print the ASCII value of all digits of N. Examples: Input: N = 8Output: 8 (56)Explanation:ASCII value of 8 is 56 Input: N = 240Output:2 (50)4 (52)0 (48) Approach: Using the ASCII table shown below, the ASCII value of all the digits of N can be printed: DigitASCII V 5 min read Program to print ASCII Value of a characterGiven a character, we need to print its ASCII value in C/C++/Java/Python. Examples : Input : a Output : 97 Input : DOutput : 68 Here are few methods in different programming languages to print ASCII value of a given character : Python code using ord function : ord() : It converts the given string o 4 min read Print given sentence into its equivalent ASCII formGiven a string containing words forming a sentence (belonging to the English language). The task is to output the equivalent ASCII sentence of the input sentence. ASCII (American Standard Code for Information Interchange) form of a sentence is the conversion of each of the characters of the input st 4 min read Count and Print the alphabets having ASCII value not in the range [l, r]Given a string str, the task is to count the number of alphabets having ASCII values, not in the range [l, r]. Examples: Input: str = "geeksforgeeks", l = 102, r = 111Output: Count = 7Characters - e, s, r have ASCII values not in the range [102, 111]. Input: str = "GeEkS", l = 80, r = 111Output: Cou 6 min read Print each word in a sentence with their corresponding average of ASCII valuesGiven a sentence, the task is to find the average of ASCII values of each word in the sentence and print it with the word. Examples: Input: sentence = "Learning a string algorithm"Output:Learning - 102a - 97string - 110algorithm - 107 Approach: Take an empty string and start traversing the sentence 6 min read Program for Binary to ASCII ConversionPython program to convert binary to ASCIIIn this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en 3 min read Program to convert given Binary to its equivalent ASCII character stringGiven a binary string str, the task is to find its equivalent ASCII (American Standard Code for Information Interchange) character string. Examples: Input: str = "0110000101100010"Output: abExplanation: Dividing str into set of 8 bits as follows: 01100001 = 97, ASCII value of 97 is 'a'.01100010 = 98 9 min read Problem Related to ASCCIProgram to Convert ASCII to UnicodeIn this article, we will learn about different character encoding techniques which are ASCII (American Standard Code for Information Interchange) and Unicode (Universal Coded Character Set), and the conversion of ASCII to Unicode. Table of Content What is ASCII Characters?What is ASCII Table?What is 4 min read Program to Convert Unicode to ASCIIGiven a Unicode number, the task is to convert this into an ASCII (American Standard Code for Information Interchange) number. ASCII numberASCII is a character encoding standard used in communication systems and computers. It uses 7-bit encoding to encode 128 different characters 0-127. These values 4 min read Program to implement ASCII lookup tableASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as âaâ or â@â or an action of some sort. ASCII lookup table is a tabular representation of corresponding values associated 7 min read Program to find the product of ASCII values of characters in a stringGiven a string str. The task is to find the product of ASCII values of characters in the string. Examples: Input: str = "IS"Output: 605973 * 83 = 6059 Input: str = "GfG"Output: 514182 The idea is to start with iterating through characters of the string and multiply their ASCII values to a variable n 4 min read How to convert character to ASCII code using JavaScript ?The purpose of this article is to get the ASCII code of any character by using JavaScript charCodeAt() method. This method is used to return the number indicating the Unicode value of the character at the specified index. Syntax: string.charCodeAt(index); Example: Below code illustrates that they ca 1 min read Converting an Integer to ASCII Characters in PythonIn Python, working with integers and characters is a common task, and there are various methods to convert an integer to ASCII characters. ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text in computers. In this article, we will explore s 2 min read Check if a string contains uppercase, lowercase, special characters and numeric valuesGiven string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then print "Yes". Otherwise, print "No". Examples: Input: str = "#GeeksForGeeks123@" Outpu 5 min read How to remove all non-alphanumeric characters from a string in JavaGiven string str, the task is to remove all non-alphanumeric characters from it and print the modified it. Examples: Input: @!Geeks-for'Geeks,123 Output: GeeksforGeeks123 Explanation: at symbol(@), exclamation point(!), dash(-), apostrophes('), and commas(, ) are removed.Input: Geeks_for$ Geeks?{}[] 3 min read Convert the ASCII value sentence to its equivalent stringGiven a string str which represents the ASCII (American Standard Code for Information Interchange) Sentence, the task is to convert this string into its equivalent character sequence. Examples: Input: str = "71101101107115" Output: Geeks 71, 101, 101, 107 are 115 are the unicode values of the charac 5 min read Check whether the given character is in upper case, lower case or non alphabetic characterGiven a character, the task is to check whether the given character is in upper case, lower case, or non-alphabetic character Examples: Input: ch = 'A'Output: A is an UpperCase characterInput: ch = 'a'Output: a is an LowerCase characterInput: ch = '0'Output: 0 is not an alphabetic characterApproach: 11 min read Average of ASCII values of characters of a given stringGiven a string, the task is to find the average of ASCII values of characters in the string. Examples: Input: str = "for"Output: 109'f'= 102, 'o' = 111, 'r' = 114(102 + 111 + 114)/3 = 109 Input: str = "geeks" Output: 105 Source: Microsoft Internship Experience Approach: Start iterating through chara 5 min read Sums of ASCII values of each word in a sentenceWe are given a sentence of English language(can also contain digits), we need to compute and print the sum of ASCII values of characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASCII each character 7 min read Program to find the largest and smallest ASCII valued characters in a stringGiven a string of lowercase and uppercase characters, your task is to find the largest and smallest alphabet (according to ASCII values) in the string. Note that in ASCII, all capital letters come before all small letters. Examples : Input : sample stringOutput : Largest = t, Smallest = aInput : Gee 6 min read Count characters in a string whose ASCII values are primeGiven a string S. The task is to count and print the number of characters in the string whose ASCII values are prime. Examples: Input: S = "geeksforgeeks" Output : 3 'g', 'e' and 'k' are the only characters whose ASCII values are prime i.e. 103, 101 and 107 respectively. Input: S = "abcdefghijklmnop 6 min read Check for ASCII String - PythonTo check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria.Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method, 2 min read Check if a String Contains Only Alphabets in Java using ASCII ValuesGiven a string, now we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than "a-z" or "A-Z". If we traverse th 4 min read Map function and Dictionary in Python to sum ASCII valuesWe are given a sentence in the English language(which can also contain digits), and we need to compute and print the sum of ASCII values of the characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASC 2 min read Count of camel case characters present in a given stringGiven a string S, the task is to count the number of camel case characters present in the given string. The camel case character is defined as the number of uppercase characters in the given string. Examples: Input: S = "ckjkUUYII"Output: 5Explanation: Camel case characters present are U, U, Y, I an 7 min read Sort an array of strings in increasing order of sum of ASCII values of charactersGiven an array arr[] consisting of N strings, the task is to sort the strings in increasing order of the sum of the ASCII (American Standard Code for Information Interchange) value of their characters. Examples: Input: arr[] = {"for", "geeks", "app", "best"}Output: app for best geeksExplanation:Sum 7 min read Count pairs of characters in a string whose ASCII value difference is KGiven string str of lower case alphabets and a non-negative integer K. The task is to find the number of pairs of characters in the given string whose ASCII value difference is exactly K. Examples: Input: str = "abcdab", K = 0 Output: 2 (a, a) and (b, b) are the only valid pairs.Input: str = "geeksf 7 min read Count of alphabets having ASCII value less than and greater than kGiven a string, the task is to count the number of alphabets having ASCII values less than and greater than or equal to a given integer k. Examples: Input: str = "GeeksForGeeks", k = 90Output:3, 10G, F, G have ascii values less than 90.e, e, k, s, o, r, e, e, k, s have ASCII values greater than or e 11 min read Count strings having sum of ASCII values of characters equal to a Prime or Armstrong NumberGiven an array arr[] of size N containing strings, the task is to count the number of strings having the sum of ASCII (American Standard Code for Information Interchange) values of characters equal to an Armstrong Number number or a Prime Number. Examples: Input: arr[] = {"hello", "nace"}Output:Numb 12 min read Make all characters of a string same by minimum number of increments or decrements of ASCII values of charactersGiven a string S of length N, the task is to make all characters of the string the same by incrementing/decrementing the ASCII value of any character by 1 any number of times. Note: All characters must be changed to a character of the original string. Examples: Input: S = "geeks"Output: 20Explanatio 6 min read Minimize swaps of same-indexed characters to make sum of ASCII value of characters of both the strings oddGiven two N-length strings S and T consisting of lowercase alphabets, the task is to minimize the number of swaps of the same indexed elements required to make the sum of the ASCII value of characters of both the strings odd. If it is not possible to make the sum of ASCII values odd, then print "-1" 9 min read Count the number of words having sum of ASCII values less than and greater than kGiven a string, the task is to count the number of words whose sum of ASCII values is less than and greater than or equal to given k. Examples: Input: str = "Learn how to code", k = 400Output:Number of words having the sum of ASCII less than k = 2Number of words having the sum of ASCII greater than 11 min read Minimum cost to make the String palindrome using ASCII valuesGiven an input string S, the task is to make it a palindrome and calculate the minimum cost for making it a palindrome, where you are allowed to use any one of the following operations any number of times: For replacing the current character with another character the cost will be the absolute diffe 5 min read Find frequency of each digit 0-9 by concatenating ASCII values of given stringGiven string str, the task is to find the frequency of all digits (0-9) in a string created by concatenating the ASCII values of each character of the given string str. Example: Input: str = "GeeksForGeeks"Output: 7 21 0 0 1 2 0 5 0 0Explanation: The array of ASCII values of all characters of the gi 5 min read Super ASCII String Checker | TCS CodeVitaIn the Byteland country, a string S is said to super ASCII string if and only if the count of each character in the string is equal to its ASCII value. In the Byteland country ASCII code of 'a' is 1, 'b' is 2, ..., 'z' is 26. The task is to find out whether the given string is a super ASCII string o 8 min read Like