Size Reserve in Type Conversion
-
iam trying to compress Xml File Using huffman Compression the code is Working and the out put is (0's,1's ) , and the Method Should Make the File Size less than the original One I think i have done something wrong in type Conversions Here i followed Some Codes i found on google , Thx in advance
QString file_name=QFileDialog::getSaveFileName(this,"Select a file","C://",tr("All files (*.*);;Huffman Files (*.huff)")); QSaveFile file(file_name); file.open(QIODevice::WriteOnly); QString text =ui->plainTextEdit->toPlainText(); string x=text.toStdString(); string Result=Huffman_encoding(x); QByteArray y=QByteArray::fromStdString(Result); file.write(y); file.commit();
-
Where is it related to Qt? What is your question?
Probably nobody will fix code you copied from elsewhere and which is not working.
Btw: The code you've posted doesnt look too wrong.
You should check thehuffman_encoding
function or debug to see what happens exactly. -
@Zeyad-Khaled Why don't you use https://doc.qt.io/qt-5/qbytearray.html#qCompress ?
-
The implied problem is, I guess, "the Method Should Make the File Size less than the original One," and it is not.
Naive Huffman coding can produce outputs slightly larger than the input for certain quite unlikely inputs: XML input is going to be compressible. However, if you actually output the characters '0' and '1' rather than bytes packed with 0/1 bits, or some diagnostic info about the Huffman tree, then the output will certainly be bigger or unexpected.
No way for us to tell without the implementation details of "Some Codes i found on google."
-
And one more comment - are you sure encode() returns a std::string and not an array of bytes? What does this function do? When it compress data then std::string is for sure wrong.
-
@Zeyad-Khaled
That's not a job/facility provided in Qt. If you have strings of digits and you want to convert to bits and pack, that's just straight C++ to write. -
@Zeyad-Khaled said in Size Reserve in Type Conversion:
The Huffman Function Returns string in 0's and 1's
If this routine returns a string of '0' and '1' characters, e.g. "01011101...", then your best course of action would be to write, find, or adapt an implementation that returns a bunch of bytes (QByteArray). In the case of the example the first byte in the array should be 0x5D.