Java 哈夫曼解码错误字节
我尝试了很多不同的搜索和论坛,我去了教授办公室。但他和我谈了5分钟,建议不要做这个额外的学分分配Java 哈夫曼解码错误字节,java,byte,decode,decoding,huffman-code,Java,Byte,Decode,Decoding,Huffman Code,我尝试了很多不同的搜索和论坛,我去了教授办公室。但他和我谈了5分钟,建议不要做这个额外的学分分配 Huffman algorithms pretty straight forward. But decoding is a bit difficult. public class DecodeMain { public static void main(String[] args) throws IOException { FileReader in = nul
Huffman algorithms pretty straight forward. But decoding is a bit difficult.
public class DecodeMain {
public static void main(String[] args) throws IOException {
FileReader in = null;
String codesFileName = "codes.txt";
Map<String, Character> bin_string_map = new HashMap<String, Character>();
try {
in = new FileReader(codesFileName);
int c;
StringBuilder message = new StringBuilder();
// read characters from the file into a string
while ((c = in.read()) != -1) {
message.append((char) c);
}
in.close();
// split string by comma + space
String[] splitted = message.toString().split(", ");
// for all except 1st and lastS
for (int i = 1; i < splitted.length - 1; i++) {
// key substring after '=' and value first char
bin_string_map.put(splitted[i].substring(2),
splitted[i].charAt(0));
}
bin_string_map.put(splitted[0].substring(3), splitted[0].charAt(1));
bin_string_map.put(splitted[splitted.length - 1].substring(2),
splitted[0].charAt(0));
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
Path path = Paths.get("compressed.txt");
byte[] data = Files.readAllBytes(path);
StringBuilder sb = new StringBuilder();
// for (int i = 0; i < data.length; i++) {
// sb.append(Integer.toBinaryString(data[i]));
// }
System.out.println(Integer.toBinaryString(data[0]));
System.out.println(bin_string_map.get("1011101"));
// T=101100101
// h=0011
// e=000
// byte[0] 1011101
// byte[1] 11111111111111111111111110110011
// byte[2] 110111
// System.out.println(sb.toString());
// StringBuilder text = new StringBuilder();
// int j = 0;
// for (int i = 0; i < sb.toString().length(); i++) {
// String key = sb.toString().substring(j, i);
// if (bin_string_map.containsKey(key)) {
// text.append(bin_string_map.get(key));
// j = i;
// }
// }
// System.out.println(text.toString());
}
}
你确定编码的文本完全相同吗?请记住,所使用的代码取决于字符的频率,文本中的微小差异可能会改变所使用的代码。(我有点怀疑您的字节值是否为111111111111101001011。一个字节是8位。)热舔,我确信它适合编码。因为compressed.txt的大小与professor compressed.txt的大小一样精确,所以compressed.txt的压缩比也精确到56%。11111111111111110110011的字节数为-77,因此增加了额外的111。我找到了一个解决方案,感谢teger.tobinarysting((数据[1]&0xFF)+0x100)。子字符串(1)
// T=101100101
// h=0011
// e=000
but bytes are:
// byte[0] 1011101
// byte[1] 11111111111111111111111110110011
// byte[2] 110111