1
import java.io.*;
public class Main2 {
    public static void main(String[] args) throws Exception {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("Text.t"));
        dos.writeByte(10101010);
        DataInputStream dis = new DataInputStream(new FileInputStream("Text.t"));
        int line;
        System.out.println(dis.readByte());
        dos.close();
        dis.close();
    }
}

i am trying to write 10101010 in a binary file that i create and print its content. When i run this it shows 18.. why? why not 10101010?Moreover when i open the Text.t file with textpad it contains this "rubbish" and not 10101010.

5 Answers5

2

10101010 % 256 = 18. This is the low byte of the integer you created. The text file is filed with "rubbish" because you saved this as binary data not as text.

If you want your number to be saved as a binary string, you should use a FileWriter and a FileReader rather than a DataInput/OutputStream, and use Integer.toBinaryString(int) and Integer.parseInt(str,2);

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • As i say to newbiedoodle what can i do in order to write this binary code and to just be this... not an arrow.. just 10101010. so when i print this to be 10101010 –  Jan 07 '15 at 17:45
  • Yes but with Filewriter every zero and one will be counted as byte and not as bit.. –  Jan 07 '15 at 18:45
  • @Panagiotis123 You can't have it both ways. Either you save it as binary and you can't read it as text or you save it as text. And by the way the actual file size (ie actual space taken up on your HD) is likely to be the same anyway (due to the way file tables work) unless you're saving thousands of numbers – ControlAltDel Jan 07 '15 at 19:00
  • For the actual size yes you are right... 1kb both .. I have a table which contains paths for each letter.. for example for letter a path is 0101 for b 010000 etc... so i want in some way to write the path in the binary(path is a string) and when i read it (in the decompression..) i want to connect the bits/path of the binary file with the letters... there is not any way to write the string in the binary file to take the 0/1 as bits and when i read it to be the same 0/1? –  Jan 07 '15 at 19:07
  • If you want to directly read the binary as a String and you don't need to do manipulation, use a `FileWriter`. If you want to write binary numbers, prefix the number with `0b` or write the decimal equivalent, so the compiler reads it in binary. – Nic Jan 07 '15 at 19:18
  • @newbiedoodle If i just write dos.writeByte(0b10101010); with all the other rows untouched it shows -86 –  Jan 07 '15 at 19:31
  • @Panagiotis123 Remember to read it out as binary, too, and not as a character. Computers do what you tell them to, not what you want them too -- if you tell it to write binary, then read as ASCII, it's not going to output what you want. – Nic Jan 07 '15 at 19:33
  • @Panagiotis123 And are you outputting it as a `byte`? That changes everything. – Nic Jan 07 '15 at 19:38
  • As i told you my code is the same as this in the post with the change that dos.writeByte(10101010); becomes dos.writeByte(0b10101010); –  Jan 07 '15 at 19:39
0

It's because you're printing binary into it, then reading the binary out. That binary code 10101010 corresponds to the arrow you're seeing, but when it's read back out, it's processed by Java and so comes out as a number, rather than the ASCII (or Unicode, depending on how it's encoded by default) representation.

Nic
  • 6,211
  • 10
  • 46
  • 69
0

You are writing a single byte to file Text.t. If you open that file in a text editor then you will see a single-character representation of that byte, according to whatever character encoding the editor decides to use.

If you want to see an eight-digit string of zeroes and ones then you want a text file, not a binary file, and you should output the data as a String.

Moreover, Java does not recognize your number as a binary literal. The value 10101010 is a decimal literal representing a number slightly greater than ten million. It will be truncated to a single byte when passed to dos.writeByte(). If you really want to output the single byte whose binary representation is 10101010, then you can express it as 0b10101010, or as a hexadecimal literal:

dos.writeByte(0b10101010);

or

dos.writeByte(0xAA);
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Java does support binary literals: http://docs.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html – ryanyuyu Jan 07 '15 at 17:50
  • Yes 10101010 was just an example i am writing Huffman code where i would see lots of 01010101...Moreover i dont want to write string because every one or zero will be counted as byte and not as bit –  Jan 07 '15 at 18:42
  • Thanks @ryanyuyu, I guess I've been living in the past. Answer updated to account for the possibility of using a binary literal. – John Bollinger Jan 07 '15 at 20:07
0

Opening a file or other datastream as binary does not mean that your integer operations now all take inputs with values expressed with the binary number system. It simply means that the data you're working with is not data likely to be read by a human.

The writeByte method on DataOutputStream takes an int input and writes it out as a byte. Therefore, your call dos.writeByte(10101010); writes the least significant 8 bits of the decimal integer value 10101010. This value happens to be 0b00010010, which is binary for 18. This is where that is coming from.

If you want to express the binary value 0b10101010 in your file, you need to write the integer value 170: dos.writeByte(170) or dos.writeByte(0xaa).

Your call to readByte suffers from the same problem. It reads a byte and System.out.println will output whatever character expresses that byte. If you want to print out the binary value of a byte, see this other SO question.

Community
  • 1
  • 1
dho
  • 2,310
  • 18
  • 20
0

Since your are calling writeByte() on a decimal integer you are getting unexpected results. WriteByte() writes an integer in the space of 1-byte, or 8 bits. The decimal number 10101010 is the same as the binary number 1001 1010 0010 0001 0001 0010 whose final 8 bits are 0001 0010 which is 18 as a decimal number.

To fix this, have java interpret your 10101010 as a binary number with 0b prefixing it to turn it into a binary literal.

dos.writeByte(0b10101010); 

Then to read this data back as a decimal string, you could use

Byte b = (dis.readByte());
String decimalString = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
System.out.println(decimalString);

The string conversion courtesy of João Silva for this question

Community
  • 1
  • 1
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
  • @Panagiotis123 Whoops. Forgot to change the data being read back to a string. Let me edit my answer. – ryanyuyu Jan 07 '15 at 19:37
  • It still has a problem if the binary code begins with 0 its false. For example 01010101 shows it as 1010101 –  Jan 07 '15 at 19:42
  • @Panagiotis123 I think it was a sign problem, try this. I haven't tested any of it. – ryanyuyu Jan 07 '15 at 20:01
  • b1 & 0xFF there you mean b & 0xFF? And with 01011010 it shows 00001000 –  Jan 07 '15 at 20:05
  • Yeah `b1`. Sorry, I'm not really going to keep trying to fix this. Sorry my answer isn't helping. Do you want me to delete it? Or do you still need something out of it for reference? – ryanyuyu Jan 07 '15 at 20:07
  • Eh, I just wanted to make sure this wasn't cluttering up your answers. Best of luck. – ryanyuyu Jan 07 '15 at 20:10