Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, July 2007
QByteArray to QString in hex format


Message 1 in thread

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 


--
 [ signature omitted ] 

Message 2 in thread

1a.) 

What about   "0x" + QString::number(myUInt , 16); ???

1b.)

QString myQString;
QTextStream myStream(&myQString);
myStream << "0x" << hex << myUInt;

2.) 

Only the "do-it-yourself-way" - see 1b.) :-(

3.) 

If you use QDataStream, it does. It depends on what you want to do. 


Regards,
Malte


"Christophe Bismuth" <christophe.bismuth@xxxxxxxxx> schrieb am 27.07.2007 
10:38:14:

> 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/
> 

--
 [ signature omitted ] 

Message 3 in thread

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 


--
 [ signature omitted ] 

Message 4 in thread

... 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/
> 

--
 [ signature omitted ] 

Message 5 in thread

On 27.07.07 17:32:42, brian li wrote:
> 
> You should implement the convert function by yourself.

Why do people always re-invent the wheel when there's already a solution
available? Instead of writing down all this code a short look into
QString's API docs reveals:

number(int, int base)

which gives you (or the OP) everything thats needed.

Andreas

-- 
 [ signature omitted ] 

Message 6 in thread

Thank you all, it works fine now with your solutions :o)

"Andreas Pakulat" <apaku@xxxxxx> a écrit dans le message de news: 
20070727105709.GD24887@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> On 27.07.07 17:32:42, brian li wrote:
>>
>> You should implement the convert function by yourself.
>
> Why do people always re-invent the wheel when there's already a solution
> available? Instead of writing down all this code a short look into
> QString's API docs reveals:
>
> number(int, int base)
>
> which gives you (or the OP) everything thats needed.
>
> Andreas
>
> -- 
> Cheer Up!  Things are getting worse at a slower rate.
>
> --
> 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/ 


--
 [ signature omitted ] 

Message 7 in thread

> -----Original Message-----
> From: Andreas Pakulat [mailto:apaku@xxxxxx]
> Sent: Friday, July 27, 2007 3:57 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: QByteArray to QString in hex format
> 
> On 27.07.07 17:32:42, brian li wrote:
> >
> > You should implement the convert function by yourself.
> 
> Why do people always re-invent the wheel when there's already a
solution
> available? Instead of writing down all this code a short look into
> QString's API docs reveals:
> 
> number(int, int base)
> 
> which gives you (or the OP) everything thats needed.
> 
> Andreas

Because We are much smarter then everyone else :)

So if I can I would much rather prefer to re-write everything for every
project..

Infact... I just was able to drop using outlook and my ISPs SMTP server.
Instead Im using my new email client and a custom SMTP server...

Dang... looks like it just got hacked... I guess back to the drawing
board...


--
 [ signature omitted ]