2017-04-20 6 views
0

현재 텍스트 파일을 압축/압축 풀기 위해 허프만 트리를 사용하고 있습니다. 현재 내 문제는 바이트를 쓰고 읽을 때 내 숫자에서 0을 잃는 것입니다.Integer.toBinaryString() 선행 0 손실

내 OutputStream 클래스에서 내 writeBit() 메서드는 한 번에 한 비트 씩 공급되고 비트 수가 8에 도달하면 바이트를 파일에 씁니다. 비트를 실제로 쓸 때 문제가 발생하지만 현재 이진수를 작성하기 위해 String을 사용합니다.

HuffmanOutputStream.java :

/** 
* Created by Sully on 3/20/2017. 
*/ 

import java.io.IOException; 


public class HuffmanOutputStream extends BitOutputStream { 

private int count = 0; 
private String bytes = ""; 

public HuffmanOutputStream(String filename, String tree, int totalChars) { 
    super(filename); 
    try { 
     d.writeUTF(tree); 
     d.writeInt(totalChars); 
    } catch (IOException e) { 
    } 
} 

public void writeBit(int bit) { 
    //PRE bit == 0 || bit == 1 
    if (count < 8) { 
     bytes += bit; 
     count++; 
    } 
    try { 

     if (count == 8) { 
      d.writeByte(Integer.parseInt(bytes, 2)); 
      count = 0; 
      bytes = ""; 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 


public void close() { 

} 
} 

일이 잘못 갈 때의 예는, 내 텍스트 파일, 내가 만드는 첫 번째 바이트는 01100001입니다 비록 내가있는 Integer.parseInt 사용할 때 (바이트, 2), 주어진 정수는 97이며 이진수로 읽으면 1100001 만 반환합니다. Huffman Trees가 포함 된 0에 의존하기 때문에 어떻게 0을 유지할 수 있습니까? 또한 0이 제자리에 남아 있는지 정확히 읽는지 확인하십시오.

HuffmanInputStream.java :

/** 
* Created by Sully on 3/20/2017. 
*/ 

import java.io.IOException; 

public class HuffmanInputStream extends BitInputStream { 
    private String tree; 
    private int totalChars; 

    private int currentByte; 
    private int bitCount; 
    private static final int BYTE_SIZE = 8; 
    private int[] bufferedBits = new int[BYTE_SIZE]; 


public HuffmanInputStream(String filename) { 
    super(filename); 

    try { 
     tree = d.readUTF(); 
     totalChars = d.readInt(); 
     currentByte = 0; 
     bitCount = 8; 

    } catch (IOException e) { 
    } 
} 


public int readBit() { 

    if (currentByte == -1) { 
     return -1; 
    } 


    if (bitCount == 8) { 
     try { 
      currentByte = d.read(); 
      if(currentByte == -1){ 
       return -1; 
      } 
      String binary = Integer.toBinaryString(currentByte); 
      for (int x = 0; x < binary.length(); x++) { 
       bufferedBits[x] = Character.valueOf(binary.charAt(x)); 
      } 
      bitCount = 0; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    int val = bufferedBits[bitCount]; 

    bitCount++; 

    return val % 2; 


} 

public String getTree() { 
    return tree; 
} 

public int totalChars() { 
    return totalChars; 
} 

public void close() { 
    try { 
     d.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

나는이 질문의 약간 긴 것을 알고 있지만 어떤 도움이 크게 감사합니다!

+0

'Integer.toBinaryString'으로 문제가 발생한 것 같습니다. –

+0

@LouisWasserman 문제가 무엇인지 설명해 주시겠습니까? 나는 왜 내가이 0들을 잃고 있는지 이해하려고 노력하고있다. 일종의 패딩/비트 이동이 필요합니까? –

+1

이것은 숫자 나 패딩이나 비트 시프 팅과 관련이 없으며, 'int'에서 0과 1의 순서로 변환하는 수단으로'Integer.toBinaryString'을 선택하는 것과 관련이 있습니다. 'Integer.toBinaryString'는 선행 0을 전혀 생성하지 않습니다. –

답변

2

0의 길이가 String이고 길이가 Integer#toBinaryString 8이되도록하려는 것으로 가정합니다. 다음 코드는 이것을 달성 할 것입니다 :

String binary = String.format("%8s", Integer.toBinaryString(currentByte)).replace(' ', '0'); 
+0

정말 고마워! 당신이 대답 할 때이 비슷한 다른 해결책을 발견했습니다. 다시 한번 감사드립니다. –

+0

여러분을 환영합니다! –