Binary to Text in Java

Binary to text in Java

You can use Integer.parseInt with a radix of 2 (binary) to convert the binary string to an integer:

int charCode = Integer.parseInt(info, 2);

Then if you want the corresponding character as a string:

String str = new Character((char)charCode).toString();

How to convert binary to text in Java?

Swap the 5 and the 1 around so it grabs the binary out of the correct textbox.

And use StringTokenizer to process each block of 8

Dont forget to import StringTokenizer

    else
{
System.out.println(jTextArea5.getText( ));
String binary = jTextArea5.getText();
StringTokenizer st = new StringTokenizer(binary," ");
while(st.hasMoreTokens()){
int ascii = Integer.parseInt(st.nextToken(), 2);
char character = (char)ascii;
jTextArea1.setText(jTextArea1.getText() + "" + character);

}
}

this bit checks whats in jTextArea5.getText()

if (code == null) {

change it to

if (code.equals("")) {

and make sure you clear out whats in the text boxes before you start either conversion

java: converting binary to text?

instead of

String out = new Character((char)k).toString();

do

String out = String.valueOf(k);

EDIT:

String input = "011000010110000101100001";
String output = "";
for(int i = 0; i <= input.length() - 8; i+=8)
{
int k = Integer.parseInt(input.substring(i, i+8), 2);
output += (char) k;
}

Converting text to binary in java

What you're really saying is "How can I pad my binary string representation of a character to 7 digits"?

Replace this line:

binaryResult += Integer.toBinaryString( (int) c);

With these:

String binString = Integer.toBinaryString( (int) c );
binaryResult += ("0000000" + binString).substring(binString.length());

This presumes that you only have 7-bit characters... if you need more, then add 0's to the "00000" string to match the length of string (with padded 0s) you want.

Trouble converting binary to text

In the line

String content = new String(sc.next().getBytes(),"UTF-8");

You already have your desired output. Here you already parsed the byte array you got to a String with the encoding UTF-8. After that you tried to decode it again into UTF-8 and hence you got a wrong result.

Edit:

Since the content of your File is written in binary, this will not be enough, you will have to parse every byte once. The problem in your for loop is, that you move the i always just one digit instead of 8 digits to the right in the binary string.

for (int i=0;i<=content.length()-8;i = i+8)

This should do the job, for real this time

JAVA converting from String to Binary and then back to String in file

I didnt go into detail on how to read/write files, since you can look that up. However, to achieve what you want:

  • you will need a method to convert a word to binary
  • then you will need another method to convert from binary to words

below is the code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HelloWorld{

public static void main(String []args) throws FileNotFoundException {
File file = new File("input.txt");
Scanner sc = new Scanner(file);
String word=sc.nextLine();
String receivedBinary=stringToBinary(word);
System.out.println();
String receivedWord=BinaryToString(receivedBinary);
}
public static String stringToBinary(String text)
{
String bString="";
String temp="";
for(int i=0;i<text.length();i++)
{
temp=Integer.toBinaryString(text.charAt(i));
for(int j=temp.length();j<8;j++)
{
temp="0"+temp;
}
bString+=temp+" ";
}

System.out.println(bString);
return bString;
}
public static String BinaryToString(String binaryCode)
{
String[] code = binaryCode.split(" ");
String word="";
for(int i=0;i<code.length;i++)
{
word+= (char)Integer.parseInt(code[i],2);
}
System.out.println(word);
return word;
}
}


Related Topics



Leave a reply



Submit