[Qt-creator] Encode with Base 64 of QByteArray

Andre Poenitz andre.poenitz at mathematik.tu-chemnitz.de
Tue May 12 23:50:14 CEST 2009


On Tue, May 12, 2009 at 01:21:18PM -0700, gn pr wrote:
> I have this code : 
> 
> QByteArray l ("D:\\grace.jpg");
> 
> #include <QtCore/QCoreApplication>
> int main(int argc, char *argv[])
> {
>   QCoreApplication a(argc, argv); 
>   
>   FILE *  origen;
>   
>   origen = fopen("d:\\grace.jpg", "rb");
>   
>   unsigned char * linea;
>   
>   QByteArray * _content = new QByteArray();

[There is no need to dynamically allocate a QByteArray locally]

>   int _fileSize = 0 ;
>    
>   while(!feof(origen) )
>   {
> fread(linea,1,1, origen) ;
> QChar caracteres((unsigned char )*linea);
> _content->append(caracteres) ;
> _fileSize += 1;
>    }
> 
> fclose(origen) ;
> 
> QByteArray tmp =  _content->toBase64();
> 
> QByteArray tmp2 = tmp.fromBase64(tmp) ;
> 
> FILE * sal ;
> 
> sal = fopen ("copia.jpg", "ab+");
> 
> fwrite(&tmp2, _fileSize, 1, sal);

&tmp2 is the address of the QByteArray, not related to its contents.
tmp2.data() _might_ work.

Andre'

PS: Why use fread/fwrite at all, and not, say QFile::readAll()




More information about the Qt-creator-old mailing list