Example #1
0
char buildCharFromByte(ibitstream& input) {
    int tempByte = 0;
    char tempChar;
    for(double i = 0; i <= 7; ++i){
        tempByte += pow(2.0, i)*input.readBit();
    }
    tempChar = tempByte;
    return tempChar;
}
Example #2
0
void decodeData(ibitstream& input, HuffmanNode* encodingTree, ostream& output) {
    int bit;
    HuffmanNode* curr = encodingTree;
    while ((bit = input.readBit()) != -1) {
        if (bit == 0 && curr->zero != NULL)
            curr = curr->zero;
        else if (bit == 1 && curr->one != NULL)
            curr = curr->one;
        else if (bit == 0 && curr->zero == NULL) {
            output.put(curr->character);
            curr = encodingTree->zero;
        } else {
            output.put(curr->character);
            curr = encodingTree->one;
        }
    }
}
Example #3
0
void decodeDataHelper(ibitstream& input, HuffmanNode* encodingTree, ostream& output){
    //Noden är ett löv, skriv ut värdet
    if(encodingTree->isLeaf()){
        int character = encodingTree->character;
        if(character != PSEUDO_EOF){    //Om vi inte nått slutet
            output.put(encodingTree->character);
        }else{
            input.get();                //Ta bort sista tecknet
        }
    }else{
        //Läs in första biten
        int tempBit = input.readBit();
        //Traversera vänster/höger träd beroende på biten
        if(tempBit == 0){
            decodeDataHelper(input, encodingTree->zero, output);
        }else if(tempBit == 1){
            decodeDataHelper(input, encodingTree->one, output);
        }
    }
}
int getCharacter(ibitstream& input, HuffmanNode* tree) {
	if (tree->isLeaf()) return tree->character;

    int bit = input.readBit();
    return (bit == 1) ? getCharacter(input, tree->one) : getCharacter(input, tree->zero);
}