I don't know what you mean that you want to "see binary" ("Matrix"?). But since you are using QDataStream you will probably get something else than you want.
I don't know what you mean that you want to "see binary" ("Matrix"?). But since you are using QDataStream you will probably get something else than you want.
Ok maybe I should elaborate a bit more. I'm writing a huffman encoding program to compress a text file. The problem I'm having is that my encoded files are coming out bigger than the original text input file, which led me to believe that my syntax was wrong.
Also I think I took the term binary file too literally which is why I expected to see 0's and 1's when I opened it.
Here is the code for my encoding function.
Qt Code:
void MainWindow::huffEncode(QMap<QChar, QBitArray> codeMap) { for (int i = 0; i < usrText.length(); i++) // Iterate over user input string { QMap<QChar, QBitArray>::iterator j; for (j = codeMap.begin(); j != codeMap.end(); j++) // iterate over codeMap (QMap of characters to huffman codes) { // comparison? if (symbol == j.key()) // if the symbol is equal to the key of the codeMap { out << j.value(); //add the huffman code of the character to the file } } } file.close(); }To copy to clipboard, switch view to plain text mode
But why are you using QDataStream? Do you know what it does? It is not a transparent general purpose binary streamer but rather a serialization mechanism. Furthermore, you can't write incomplete bytes into a file. There is no way to write four bits into a file, you need to write a whole byte, no filesystem I know of allows files with sizes of fractions of a byte.
The reason I'm using it is because my lecturer told me to. He also said that QDataStream would automatically pad bits for QBitArray in order to make up a whole byte.
To write "raw" data to a QDataStream you must use "writeRawData". Otherwise, the data written is only readable by another instance of QDataStream.
Ask him why he did that then. It's not a correct approach.
Doesn't it defeat the purpose of using huffman code? Besides, your lecturer is wrong.He also said that QDataStream would automatically pad bits for QBitArray in order to make up a whole byte.
If you write a four bit array to data stream, 40 bits will be written to the device. And no padding is there, actually. The superflous bits are just ignored, from what I see.Qt Code:
{ quint32 len = ba.size(); out << len; if (len > 0) out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1); return out; }To copy to clipboard, switch view to plain text mode
Bookmarks