Search code examples
c++file-iohuffman-codebitset

How to convert from bitset back to int


So I currently working on file compression so to compress the file I am converting my string of ints using huffman encoding

example:

1000011011010100011010100010001101111001001111010011000011011100001010010110011011 1001110011111000010110110101111111

to bit set like this :

int i = 0;
while (i < str.length())
{
    bitset<8>set(str.substr(i, i + 7));
    outputFile << char(set.to_ulong());
    i = i + 8;
}

when I am retrieving the contents of that file I cant figure out how to convert it back to a string of ints. Once i get back the string of ints I can retrieve the original contents I encoded its just getting the string back thats the problem


Solution

  • If I correctly understand your question here is what you can do to perform a reversed operation:

    constexpr int bits_num = sizeof(unsigned char) * CHAR_BIT; //somewhere upper in the file
    //...
    
    std::string outputStr;
    unsigned char c;
    while (inputFile.get(c))  //the simplest function to read "character by character"
    {
        std::bitset<bits_num> bset(c);  //initialize bitset with a char value
        outputStr += bset.to_string();  //append to output string
    }
    

    Of course I assume that inputFile stream you will declare and set up for yourself.

    sizeof(unsigned char) * CHAR_BIT is an elegant way of expressing a number of bits in a variables's type (like here unsigned char). To use the constant CHAR_BIT (which is basically 8 on all modern architectures) add #include <climits> header.


    You have also an error in your code:

    bitset<8>set(str.substr(i, i + 7));
    

    You're increasing the cut out substring each iteration (i + 7), change it simply to 8 (or better and less error-prone to the suggested constant):

    constexpr int bits_num = sizeof(unsigned char) * CHAR_BIT;
    
        //...
        std::bitset<bits_num> set( str.substr(i, bits_num) );
        outputFile << static_cast<unsigned char>( set.to_ulong() );
        i += bits_num;