qt-interest@trolltech.com
[Top] [All Lists]

RE: QByteArray to QString in hex format

Subject: RE: QByteArray to QString in hex format
From: "Malte Witt"
Date: Fri, 27 Jul 2007 12:20:28 +0200
... or you could use the more readable:

QString bytes2String(const QByteArray & arcArray)
{
    QString myQString;
    QTextStream myStream(&myQString);
    myStream << "0x" 
             << hex << qSetFieldWidth(2) << qSetPadChar('0') << left
 
    for (int i = 0; i < arcArray.size(); ++i)
    {
        myQString << static_cast<unsigned char>(arcArray.at(i));
    }
    return myQString;
}

Regards,
Malte



"brian li" <lxgbrian@xxxxxxxxx> schrieb am 27.07.2007 11:32:42:

> 
> You should implement the convert function by yourself.
> 
> The following code is for your reference.
> 
> QString bytes2String(unsigned char* bytes, int len)
> {
>     QString rt;
>     for(int i=0; i<len; i++)
>     {
>         char l = (bytes[i]>>4)&0x0F;
>         char r= bytes[i]&0x0F;
>         QChar c[2] = { c2a(l),c2a(r)};
>         rt += QString(c,2);
>     }
>     return QString("0x") + rt;
> }
> 
> unsigned char c2a(unsigned char c)
> {
>     if(c<=9)
>     {
>         return c+0x30;
>     }
>     return c-0x0A+'A';
> }
> -----Original Message-----
> From: Christophe Bismuth [mailto:christophe.bismuth@xxxxxxxxx] 
> Sent: 2007年7月27日 4:38 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: QByteArray to QString in hex format
> 
> Dear community,
> 
> 1. I'm getting mad... I just would like to convert an integer variable 
to a 
> formatted hex string.
> With "uint myUInt = 24645;" I would like to have a QString containing 
0x6045
> 
> (with or without leading 0).
> 
> Here what I've done, but I think it's too complicated and endianess is 
> wrong...
> uint myUInt = 24645;
> QString(QByteArray((char *) &myUInt, 4).toHex()).prepend("0x");
> 
> Does anyone has a better solution?
> 
> 2. Is there a simple way to convert a QByteArray into that kind of hex 
> QString?
> 
> 3. Does Qt provides a way to swap endianess ?
> 
> Thank you,
> Christophe 
> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx 
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>