Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to decode from stream back to huffman code? #1

Open
crookedbard opened this issue Jan 14, 2017 · 0 comments
Open

How to decode from stream back to huffman code? #1

crookedbard opened this issue Jan 14, 2017 · 0 comments

Comments

@crookedbard
Copy link

In main.cs these lines show how to convert huffman code (vector of bools) to the stream.
But how to convert the stream back to vector of bools?

short r = code.size()%8;
    long bsize = ((code.size()-r)/8);
    
//I am keeping the stream in memory.    
//std::ofstream outfile ("out.huf",std::ofstream::binary);
   std::stringstream outbuff;
    
    unsigned char byte[1];
    unsigned char firstByte[1];
    
    firstByte[0] = 0;
    if((r-(r%4))/4) firstByte[0] |= 1 << 2;
    if(((r%4)-(r%2))/2) firstByte[0] |= 1 << 1;
    if(r%2) firstByte[0] |= 1;    
    
    outfile.write (reinterpret_cast<char *>(firstByte),1);
   
    for(int y=0;y<(bsize+1);y++) { //byte
        byte[0] = 0;
        for (int i=0; i < 8; ++i) { //bit
            if ((8*y+i) <= code.size() && code[8*y+i]) {
                byte[0] |= 1 << i;
            }                  
        }
        outfile.write (reinterpret_cast<char *>(byte),1);
    }

How to decode outfile stream back to vector ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant