0

I have tried a lot with many ways to write a program that : write a one byte value in a file as it is.. for example write 01010101 in a file.. then i want to read the file and print what i wrote.So it should display 01010101. None of my codes worked so. Any help? Because i am writing a compression program it essential to be 1 byte and not 8

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main2 {
    public static void main(String[] args) throws Exception {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("Text.t"));
        dos.writeBytes(String.valueOf(01010101));
        File file = new File("Text.t");
        BufferedReader br = new BufferedReader(
            new InputStreamReader(
                new FileInputStream(file)));
        System.out.println(br.readLine());
        dos.close();
        br.close();

    }
}

It works well with binary code that starst with 1 but with 0 not.. for example for 01010101 it shows 266305

9
  • If we only could see the secret code .... Commented Jan 9, 2015 at 15:50
  • Can you edit the question and paste your code into it? Commented Jan 9, 2015 at 15:50
  • I have tried a lot .. the best is the one that i would write in my next edit that when the binary code begins with 0 it doesnt work Commented Jan 9, 2015 at 15:52
  • you are converting an int to String, so it is 8 bytes (length of String), did you want to add a byte instead? Commented Jan 9, 2015 at 16:09
  • Yes i wanted to add only 1 byte Commented Jan 9, 2015 at 16:13

2 Answers 2

2

The problem with "It works well with binary code that starst with 1 but with 0 not.. for example for 01010101 it shows 266305" is that 01010101 is an octal literal and is read in by the compiler as base-8 (aka Octal).

Use 1010101 when writing the literal - leading zeros mean nothing to numbers; but they do mean something to how the Java code is parsed!

Decimal numbers that are displayed as "00xyz" are often zero-padded, which is applied to the string representation; the number itself is xyz.


From the comment I believe the desired operation is to use a binary literal. You'll have to emit this using a "bit converter" to display as expected - the bit converter will take the value of eg. 0b11 (integer 3) and turn it into a string "11". You may also want to apply a padding with an assumed output width - again, 0b01 == 0b1 and the leading 0 means nothing to an integer.

The following will emit the decimal string representation of the huffman bit sequence, without any leading zeros. However this when paired with above should get you off on the right track.

 dos.writeBytes(String.valueOf(0b01001010));
Sign up to request clarification or add additional context in comments.

11 Comments

i am writing huffman code so every 0 or 1 is very important. i cannot write 1010101 ... for example the letter A may be encoded 00000000 .
The leading 0 of integers mean nothing. It is in your head. Any such leading 0 is an artifact of a representation of such. Usually when talking about something like huffman there is an implicit 'length' of used bits [grossly simplifying] that is shown in algorithm papers (which show the bits and not an integer value) - integers in Java have no such notion.
yes but i am telling you again.. i am writing huffman code so when i read it back when it sees 001 this may be letter b when it sees 1 this may be letter g
I'm saying you're misunderstanding integers - and the representation of such. First off, the integer value int i = 101 is gibberish to huffman encoding (well it likely means something but does not represent the tree "as it looks"). However, the sequence of bits {0,1,0,1} means something. Did you mean to use a binary literal instead?
Since you are dealing with Huffman codes, you should think about the encoded strings as a sequence of bits, not as numbers. That is, if you write to a text file "01010101" you are using 8 bytes instead of 1 which completely defeats the purpose of compression. This is because you are converting an integer to a string and then writing the bytes in that string to file. You need to write the integer (or byte) directly to the file, e.g., using writeByte.
|
0

I would use a Byte representation for radix 2 e.g. Byte.parseByte("00010001", 2). But the problem is Java's primitives are signed numbers so it won't work for negative values (when first digit is 1), thus Byte.parseByte("10010011", 2) will throw a NumberFormatException.

The trick here is to initially replace leading digit (if it is 1, with 0), parse it and then set the bit again to 1. Then store this byte to your file.

private static byte binaryStringToByte(String s) {
    //also check for null, length = 8, contain 0/1 only etc.
    if (s.startsWith("0")) {
        return Byte.parseByte(s, 2);
    } else {
        StringBuilder sBuilder = new StringBuilder(s);
        sBuilder.setCharAt(0, '0');
        byte temp = Byte.parseByte(sBuilder.toString(), 2);
        return (byte) (temp | (1 << 7));
    }
}

Then, to get the binary String representation of a byte use this code:

byte b = binaryStringToByte("10001000");
String s1 = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');

2 Comments

Your code is little difficult...what method do you suggest me to use in order to write in the file? i would write the string s1?
no, you should store the byte returned from binaryStringToByte. check this stackoverflow.com/questions/4350084/byte-to-file-in-java

Your Answer

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