Computer Science, asked by BrainlyProgrammer, 2 months ago

HeYa Brainlians!!
Challenge 4 u

Q) WAP in JAVA to convert the string input by the user and convert it to the binary cödes.

Ex:
Sample input:
A

Output:
1000001

Hint: Use ASCII côdes
_​

Answers

Answered by Oreki
10

\text{\bf\large Algorithm}

   \textsf{\textemdash \:\: Accepting a character using the \textbf{Scanner} class.}\\\textsf{\textemdash \:\: Converting it to Binary using the \texttt{Integer.toBinaryString()} method.}

\text{\bf\large Program}

    \begin{small}\texttt{import java.util.Scanner;}\\\texttt{public class CharacterToBinary \{}\\\texttt{\hspace{1em} public static void main(String[] args) \{}\\\texttt{\hspace{2em} System.out.print("Enter a character - ");}\\\texttt{\hspace{2em} char character = new Scanner(System.in).next().charAt(0);}\\\texttt{\hspace{2em} System.out.println("Binary - " + Integer.toBinaryString(character));}\\\texttt{\hspace{1em} \}}\\\texttt{\}}\\\end{small}

\text{\bf\large Variable Description}

     \text{\emph{character} (\textbf{char}) - To store a character to be converted to binary.}

Attachments:
Answered by ayanzubair
1

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;

  

//

Attachments:
Similar questions