//huff is a String that is completely made up of 0's and 1's (and its length is divisible by 8) for (int i=0; i < huff.length()*8; i+=8) { byte b = Byte.valueOf(huff.substring(i, i+8), 2).byteValue(); fos.write(b); }