Java - Problems with the Huffman Algorithm Output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Winterrage
    New Member
    • Sep 2007
    • 8

    Java - Problems with the Huffman Algorithm Output

    Hello everyone,

    I'm trying to make a compression application using the Huffman Algorithm but i'm not sure on how to write to a file the output.

    I have a binary sequence associated to every character in the input file.

    Now the problem is:
    - I create a binary string based on the input file.
    - Then I have to write the compressed file, but I have no idea on how to work with my binary string.

    (the binary string is something like:
    String binaryString = "011110110"

    any help would be appreciated

    Thanks,

    Stephane
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Winterrage
    Hello everyone,

    I'm trying to make a compression application using the Huffman Algorithm but i'm not sure on how to write to a file the output.

    I have a binary sequence associated to every character in the input file.

    Now the problem is:
    - I create a binary string based on the input file.
    - Then I have to write the compressed file, but I have no idea on how to work with my binary string.

    (the binary string is something like:
    String binaryString = "011110110"

    any help would be appreciated

    Thanks,

    Stephane
    That's not a 'binary String' that's a String that just consists of the characters '1'
    and '0'. If you group eight of these characters together you can convert them to
    a byte and write that byte to the OutputStream. Keep on repeating that until you've
    written your entire String.

    This raises the question: what do you need that String for to begin with?

    kind regards,

    Jos

    Comment

    • Winterrage
      New Member
      • Sep 2007
      • 8

      #3
      Originally posted by JosAH
      That's not a 'binary String' that's a String that just consists of the characters '1'
      and '0'. If you group eight of these characters together you can convert them to
      a byte and write that byte to the OutputStream. Keep on repeating that until you've
      written your entire String.

      This raises the question: what do you need that String for to begin with?

      kind regards,

      Jos
      Instead of going throught the tree the whole time, i've created a conversion Hashtable where you call .get(char) and that returns the associated "(not)binar y string"

      Thanks for your help still :)

      Comment

      • Winterrage
        New Member
        • Sep 2007
        • 8

        #4
        Originally posted by Winterrage
        Instead of going throught the tree the whole time, i've created a conversion Hashtable where you call .get(char) and that returns the associated "(not)binar y string"

        Thanks for your help still :)
        Having a small problem with the byte conversion still ...

        while((readedCh aracter ) != EOFChar)
        {
        binaryString += this.correspond ance.get(readed Character);
        if(binaryString .length() >=8)
        {
        String subString = binaryString.su bstring(0, 8);
        binaryString = binaryString.su bstring(8);
        Byte convertedString = new Byte(subString) ;
        out.write(conve rtedString);
        out.flush();
        }

        readedCharacter = (char)dis.read( );
        }

        I'm getting the following exception when trying to convert the string to a byte

        Exception in thread "main" java.lang.Numbe rFormatExceptio n: Value out of range. Value:"11111111 " Radix:10

        Comment

        Working...