Reputation: 95
When we create a text file with this text "ali ata bak", and we use this file as input for the program. The code is running normally. But when we enter "1111111111111111111111" this text in the textfile, Code isnt running expected. So What is the problem?
#include <QtCore/QCoreApplication>
#include <QBitArray>
#include <QByteRef>
#include <QFile>
#include <iostream>
#include <stdlib.h>
#include <QTextStream>
// Buffer Size #num of KB's
#define BUFFER_SIZE_KB 1
// Do not change !!
#define BUFFER_SIZE_BYTE BUFFER_SIZE_KB*1024
#define BUFFER_SIZE_BIT BUFFER_SIZE_BYTE*8
using namespace std;
QBitArray bytesToBits(QByteArray bytes) {
QBitArray bits(bytes.count()*8);
// Convert from QByteArray to QBitArray
for(int i=0; i<bytes.count(); ++i)
for(int b=0; b<8; ++b)
bits.setBit(i*8+b, bytes.at(i)&(1<<b));
return bits;
}
QByteArray bitsToBytes(QBitArray bits) {
QByteArray bytes;
bytes.resize(bits.count()/8);
// Convert from QBitArray to QByteArray
for(int b=0; b<bits.count(); ++b)
bytes[b/8] = ( bytes.at(b/8) | ((bits[b]?1:0)<<(b%8)));
return bytes;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString inFilename;
QString outFilename;
QTextStream qtin(stdin);
cout << "Filename : ";
qtin >> inFilename;
outFilename.append("_");
outFilename.append(inFilename);
QFile infile(inFilename);
if (!infile.open(QIODevice::ReadOnly)) {
cout << "\nFile cant opened\n";
system("pause");
return 1;
}
QFile outfile(outFilename);
if (!outfile.open(QIODevice::WriteOnly)) {
cout << "\nFile cant opened\n";
system("pause");
return 2;
}
QByteArray bytes, bytes2;
QBitArray bits;
while ((bytes = infile.read(BUFFER_SIZE_BYTE)) >0 ) {
bits = bytesToBits(bytes);
bytes2 = bitsToBytes(bits);// PROBLEM IS HERE
outfile.write(bytes2);
}
outfile.close();
infile.close();
cout << "Finished\n";
return a.exec();
}
Upvotes: 8
Views: 8973
Reputation: 11
Based on answers from iloahz, solve the problem of output file size grow up 1 byte.
QByteArray bitsToBytes(QBitArray bits) {
QByteArray bytes;
bytes.resize(bits.count() / 8 + ((bits.count() % 8)? 1: 0));
bytes.fill(0x00);
// Convert from QBitArray to QByteArray
for(int b = 0; b < bits.count(); ++b)
bytes[b / 8] = (bytes.at(b / 8) | ((bits[b]? 1: 0) << (b % 8)));
return bytes;
}
Upvotes: 0
Reputation: 4484
QBitArray bits(8);
// The deep copy of bits
QByteArray bytes(bits.bits(), bits.count() / 8);
--------------------------------------------------------------------------
// The bits are not copied
QByteArray bytes = QByteArray::fromRawData(bits.bits(), bits.count() / 8);
Upvotes: 1
Reputation: 874
QByteArray bitsToBytes(const QBitArray& bits)
{
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream << bits;
return bytes;
}
Upvotes: 1
Reputation: 56
You might also want to read about endianness from the Wikipedia http://en.wikipedia.org/wiki/Endianness
Upvotes: 0
Reputation: 4561
Initialization problem.
QByteArray bitsToBytes(QBitArray bits) {
QByteArray bytes;
bytes.resize(bits.count()/8+1);
bytes.fill(0);
// Convert from QBitArray to QByteArray
for(int b=0; b<bits.count(); ++b)
bytes[b/8] = ( bytes.at(b/8) | ((bits[b]?1:0)<<(b%8)));
return bytes;
}
this produces the right answer
Upvotes: 7