阅读和使用C滴滴写位+ +的霍夫曼编码 [英] Reading and writing bit by bit in C++ for Huffman Encoding

查看:146
本文介绍了阅读和使用C滴滴写位+ +的霍夫曼编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想带code和德code代表霍夫曼用C ++编码的。我不知道在我的问题是,我能够正确地读写,但是当我DECOM preSS其炒,所以我不是没有编码的文件或解码。
我认为它的时候我写和读的地方出问题的文件。所以这是我写的EN codeD文件。首先,我存储所有位codeS从我叫UMAP无序映射成一个字符串:

  INT I = 0,J = 0;
字符串fullStr =;
对于(i = 0; I< BUFFSIZE;我++)//把所有codeS中的1和0的一根弦
    fullStr + = UMAP [缓冲[I]];
无符号字符字节= 0;
I = 0;
为(J = 0; J< fullStr.length(); J ++)
{    如果(ⅰ!= 8)
    {
        字节| =(fullStr [J] =='1')<<一世; //组成一个字节
        我++;
    }
    其他
    {
        outf.put(字节); //一次写一个字节
        字节= 0;
        I = 0;
    }
}
如果(i = 0&安培;!&安培; I< 8)
{
    而(ⅰ&下; 8)
    {
        字节| = 0℃;&下;一世;如果未完成//完成了最后一个字节
        我++;
    }
    outf.put(字节);
}

然后在DECOM preSS端:

  INT I = 0;
无符号字符字节= 0;
位集&所述8是氢; setByte;
的ofstream OUTF(文件名,的ofstream ::二进制);
字符串连接=;
字符串bitStr =;
对于(i = 0; I< buffLength;我++)
{
    setByte =缓冲液[I];
    bitStr = setByte.to_string();
    对于(INT J = 0; J< 8; J ++)
    {
        CONCAT + = bitStr [J]。
        如果(UMAP [CONCAT])
        {
            // COUT<< 发现code<< CONCAT<< << UMAP [CONCAT<< ENDL;
            outf.put(UMAP [CONCAT]);
            CONCAT =;
        }
    }
}
outf.close();


解决方案

感谢您风向标你对他们被扭转,读我把那照顾正确的,但事实证明这是还怎么我写它太

新的COM preSS:

  INT I = 0,J = 0;
字符串fullStr =;
对于(i = 0; I< BUFFSIZE;我++)//把所有codeS在一个字符串
    fullStr + = UMAP [缓冲[I]];
对于(i = 0; I< fullStr.length(); I + = 8)
{
    无符号字符字节= 0;
    字符串STR8 =;
    如果第(i + 8示fullStr.length())
        STR8 = fullStr.substr(I,I + 8);
    其他
        STR8 = fullStr.substr(ⅰ,fullStr.length());
    为(无符号​​B = 0; B = 8;!++ B)
    {
        如果(B< str8.length())
            字节| =(STR8并[b]&放大器; 1) - ;&下; b: //这一行是以前错了
        其他
            字节| = 1&所述;&下; b:
    }
    outf.put(字节);
}
INT filelen = outf.tellp();
outf.close();

新DECOM preSS:

  INT I = 0,J = 0,K = 0;
无符号字符字节= 0;
位集&所述8是氢; setByte,reverseByte;
的ofstream OUTF(文件名,的ofstream ::二进制);
字符串连接=;
字符串bitStr =;
串反向=;
INT charCount = 0;
对于(i = 0; I< buffLength;我++)
{
    setByte =缓冲液[I];
    bitStr = setByte.to_string();
    反向=;
    对于(K = 7; K> = 0; K--)
        扭转+ = bitStr [K];
    为(J = 0; J&下; 8; J ++)
    {
        CONCAT + =反向[J]。
        如果(UMAP [CONCAT])
        {
            OUTF<< UMAP [CONCAT]
            charCount ++;
            CONCAT =;
            如果(charCount == origLength)//如果我们写原金额停
            {
                outf.close();
                返回1;
            }        }
    }
}
outf.close();
返回1;

I'm trying to encode and decode for a Huffman coding in C++. I'm not sure where my problem is I'm able to read and write but when I decompress the file its scrambled so I'm either not encoding or decoding correctly. I think its when I'm writing and reading the file where things go wrong. So this is what I have to write the encoded file. First I store all the bitcodes from my unordered map called uMap into one string:

int i = 0, j = 0;
string fullStr = "";
for (i = 0; i < buffsize; i++) //put all codes in one string of 1's and 0's
    fullStr += uMap[buffer[i]];
unsigned char byte = 0;
i = 0;
for (j = 0; j < fullStr.length(); j++)
{

    if (i != 8)
    {
        byte |= (fullStr[j] == '1') << i; // make up one byte
        i++;
    }
    else
    {
        outf.put(byte); // write one byte at a time
        byte = 0;
        i = 0;
    }
}
if (i != 0 && i < 8)
{
    while (i<8)
    {
        byte |= 0 << i; // finish up last byte if not finished
        i++;
    }
    outf.put(byte);
}

Then on the decompress side:

int i = 0;
unsigned char byte = 0;
bitset<8> setByte;
ofstream outf(filename, ofstream::binary);
string concat = "";
string bitStr = "";
for (i = 0; i < buffLength; i++)
{
    setByte = buffer[i];
    bitStr = setByte.to_string();
    for (int j = 0; j < 8; j++)
    {
        concat += bitStr[j];
        if (uMap[concat])
        {
            //cout << "found code " << concat << " " << uMap[concat] << endl;
            outf.put(uMap[concat]);
            concat = "";
        }
    }
}
outf.close();

解决方案

Thank you Weather Vane you were correct about the read they were being reversed so I took care of that but it turns out it was also how i was writing it too.

New compress:

int i = 0, j = 0;
string fullStr = "";
for (i = 0; i < buffsize; i++) //put all codes in one string
    fullStr += uMap[buffer[i]];
for (i = 0; i < fullStr.length(); i+=8)
{
    unsigned char byte = 0;
    string str8 = "";
    if (i + 8 < fullStr.length())
        str8 = fullStr.substr(i, i + 8);
    else
        str8 = fullStr.substr(i, fullStr.length());
    for (unsigned b = 0; b != 8; ++b)
    {
        if (b < str8.length())
            byte |= (str8[b] & 1) << b; // this line was wrong before
        else
            byte |= 1 << b;
    }
    outf.put(byte);
}
int filelen = outf.tellp();
outf.close();

New decompress:

int i = 0,j=0,k=0;
unsigned char byte = 0;
bitset<8> setByte, reverseByte;
ofstream outf(filename, ofstream::binary);
string concat = "";
string bitStr = "";
string reverse = "";
int charCount = 0;
for (i = 0; i < buffLength; i++)
{
    setByte = buffer[i];
    bitStr = setByte.to_string();
    reverse = "";
    for (k = 7; k>=0; k--)
        reverse += bitStr[k];
    for (j = 0; j < 8; j++)
    {
        concat += reverse[j];
        if (uMap[concat])
        {
            outf << uMap[concat];
            charCount++;
            concat = "";
            if (charCount == origLength) // if we have written original amount stop
            {
                outf.close();
                return 1;
            }

        }
    }
}
outf.close();
return 1;

这篇关于阅读和使用C滴滴写位+ +的霍夫曼编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆