Luud van Keulen
Luud van Keulen

Reputation: 1254

String of 0's and 1's to File as bits

I am working on a Huffman java application and i'm almost done. I have one problem though. I need to save a String of something like: "101011101010" to a file. When I save it with my current code it saves it as characters which take up 1 byte every 0 or 1. I'm pretty sure it's possible to save every 0/1 as a bit.

I already tried some things with BitSet and Integer.valueOf but I can't get them to work. This is my current code:

FileOutputStream fos = new FileOutputStream("encoded.bin");
fos.write(encoded.getBytes());
fos.close();

Where 'encoded' is a String which can be like: "0101011101". If I try to save it as integer the leading 0 will be removed.

Thanks in advance!

EDIT: Huffman is a compression method so the outputted file should be as small as possible.

Upvotes: 4

Views: 4640

Answers (3)

Luud van Keulen
Luud van Keulen

Reputation: 1254

I think I found my answer. I put the 1's and 0's in a BitSet using the following code:

BitSet bitSet = new BitSet(encoded.length());
int bitcounter = 0;
for(Character c : encoded.toCharArray()) {
    if(c.equals('1')) {
        bitSet.set(bitcounter);
    }
    bitcounter++;
}

After that I save it to the file using bitSet.toByteArray() When I want to read it again I convert it back to a bitset using BitSet.valueOf(bitSet.toByteArray()). Then I loop through the bitset like this:

String binaryString = "";
for(int i = 0; i <= set.length(); i++) {
    if(set.get(i)) {
        binaryString += "1";
    } else {
        binaryString += "0";
    }
}

Thanks to everyone who helped me.

Upvotes: 3

user4910279
user4910279

Reputation:

Try this.

String encoded = "0101011101";
FileOutputStream fos = new FileOutputStream("encoded.bin");
String s = encoded + "00000000".substring(encoded.length() % 8);
for (int i = 0, len = s.length(); i < len; i += 8)
    fos.write((byte)Integer.parseInt(s.substring(i, i + 8), 2));
fos.close();

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

Binary files are limited to storing bits in multiples of eight. You can solve this problem by chopping the string into eight-bit chunks, converting them to bytes using Byte.parseByte(eightCharString, 2) and adding them to a byte array:

  • Compute the length of the byte array by dividing the length of your bit string by eight
  • Allocate an array of bytes of the desired length
  • Run a loop that takes substrings from the string at positions representing multiples of eight
  • Parse each chunk, and put the result into the corresponding byte
  • Call fos.write() on the byte array

Upvotes: 0

Related Questions