2

I have a file in which its content is only 0's and 1's

I want to write a java program that convert each 8 bit of 0's and 1's to char or in other word, to convert the file content from binary to chars.

Which function can I use for that purpose?

6
  • 1
    Text files are binary files containing bytes that can be interpreted as text. In other words, if these zeros and ones are arranged according to, say, ASCII, or UICODE encoding, you don't need to do anything special, just read the file in. Commented Dec 26, 2011 at 9:47
  • No, I have to write a random 0's and 1's into a text file Then, to read from this file and print the chars on screen Commented Dec 26, 2011 at 9:49
  • +1. In other words: all files are binary files. Some binary files happen to contain bytes that represent characters in a certain encoding. Commented Dec 26, 2011 at 9:51
  • @user1077980: so your file is a text file containing only the characters '0' and '1'? Commented Dec 26, 2011 at 9:53
  • If I wrote in text file the following: 00100001 and I want to print on screen the content of the file in char for example to print "a" which function should I use? Commented Dec 26, 2011 at 9:54

3 Answers 3

8

This one is also work

    String s = "0110100001100101011011000110110001101111";
    String str = "";

    for (int i = 0; i < s.length()/8; i++) {

        int a = Integer.parseInt(s.substring(8*i,(i+1)*8),2);
        str += (char)(a);
    }

    System.out.println(str);
Sign up to request clarification or add additional context in comments.

1 Comment

Does this work when a byte has "0000" in the beginning?
2

First of al, you'll have to convert Strings of 8 '0' and '1' characters into bytes. This is easily done using Integer.parseInt(), and converting the int to a byte using

byte b = (byte) (i & 0xFF)

Then you need to create a byte array with all these bytes.

And finally, you need to transform this byte array into a String. This is where you need to decide whcih encoding to use. The same String can be transformed into different byte arrays, depending on the encoding. And all byte sequences don't represent valid characters. Suppose you want to use ASCII as encoding, the use new String(bytes, "ASCII"). But beware that all bytes bigger than 128 are not valid ASCII characters.

6 Comments

yes, it works well :) But how about using .hasNextByte() while reading from file does it work?
I tried it using : while(input.hasNextByte()) but it never enters the loop !
probably, yes, provided the radix is 2. Why don't you just test it?
while(input.hasNextByte(2)){ System.out.println("yes"); System.out.println(""+input.nextByte()); }
I don't know why it doesn't enter the looop
|
0

If you already have the file's contents in a byte array byte[] you can simply use the appropriate constructor String(byte[], Charset).

But be aware that some charsets use more than one byte per character, so you probably do not want to use UTF-8 but for example US-ASCII.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.