Hello,how can we write unsigned char to file.
i used write() but i cant
Hello,how can we write unsigned char to file.
i used write() but i cant
Since that is done using write() could you show the code that you are using and how you determine that it didn' work?
Cheers,
__
Not clear if you mean a single unsigned char or a bunch of them:
Qt Code:
unsigned char a = 0x00; unsigned char data[] = { 0x00, 0x01, 0x02, 0xff }; file.write(reinterpret_cast<char *>(&a), sizeof(a)); file.write(reinterpret_cast<char *>(&data), sizeof(data)); file.close(); }To copy to clipboard, switch view to plain text modeQt Code:
$ od -tx1 test.bin 0000000 00 00 01 02 ffTo copy to clipboard, switch view to plain text mode
seniorc (20th December 2013)
A possible cause of the problem: You can write either char * or QByteArray. Because of type checking, you cannot write an unsigned char *. Do as ChrisW67 has shown and reinterpret_cast.
You can write unsigned char *.Here is one of the my sample code
Can get idea from this:
Qt Code:
unsigned char* data3d = new unsigned char[x_size*y_size]; int i,j,k=0; fp = fopen (output_str, "wb"); for ( i = x_size; i >= 0; i--) for (j = 0; j < y_size; j++) { data3d[k++]=Buffer_in[i][j]; } fwrite( data3d, sizeof( char ),x_size*y_size, fp); fclose(fp);To copy to clipboard, switch view to plain text mode
This is ANSI C, not Qt, prkhr4u. The pointer in fwrite() is void * so that you can write anything you want. The pointer in QIODevice::write() is char * (or a reference to a QByteArray).
I done writing unsigned char to file,ChrisW67's way.But i have new problem.Here is my codes.very slow run in this line.How can i do faster than now.Any idea?
Qt Code:
QString compressed // inside bit example: 1010101010111...To copy to clipboard, switch view to plain text modeQt Code:
for (;!(compressed.isEmpty());compressed=compressed.mid(8)){ unsigned char byte = 0; for (int j=0; j<8; ++j) { if (compressed.at(j) == '1') byte |= 0x01; else byte &= ~(0x01); if (j < 7) byte<<=1; } codefile.write(reinterpret_cast<char *>(&byte),sizeof(byte)); }To copy to clipboard, switch view to plain text mode
Thanks
Last edited by seniorc; 21st December 2013 at 11:32.
My guess would be that the mid() call makes it slow due to having to create a new string and copy the remaining data.
Better loop over the whole string and write a byte every 8th character.
Additionally you could also preallocate a QByteArray with compressed.length / 8 size and fill it as you go, then write that block to the file in one go.
Cheers,
_
Make in advance everything you can do in advance and go "low level" avoiding as much of unnecessary actions you can do. For example:
Qt Code:
const int N = compressed.size(); int k = 0; unsigned char buff = 0; unsigned char bits[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; for( int i = 0; i < N; i++ ) { if( dd[i] == Zero ) k++; else if( dd[i] = One ) { buff |= bits[k]; k++; } else { report error; break; } if( k == 8 ) { ff.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char)); buff = 0; k = 0; } } if( k > 0 ) // incomplete byte { ff.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char)); set some flag, only k bits valid; }To copy to clipboard, switch view to plain text mode
seniorc (21st December 2013)
Thanks I gained twelve seconds,How can I improve better low level programming whats your suggestions,for example;thats my mistakes?I've got a new question.QMap,QHash,QList write file?I used http://doc.qt.io/qt-4.8/QDataStream but is there a better way?for example;write QMap and read QMap for easy way?
You could save yourself a bunch of manipulations if your built your binary data up front rather than building a string full of '1' and '0' characters and then having to convert it.
Bookmarks