Results 1 to 5 of 5

Thread: QString convert to QByteArray

  1. #1
    Join Date
    Dec 2014
    Posts
    4
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QString convert to QByteArray

    Hi,
    Are there any way to convert a QString as following data to QByteArray?
    I try to code as following, but it cannot work.
    Qt Code:
    1. QString test_raw1 = "0x01";
    2. QString test_raw2 = "0x8a";
    3. QByteArray tmp_byte;
    4. tmp_byte.resize(2);
    5. tmp_byte[0].insert(0,test_raw1);
    6. tmp_byte[1].insert(1,test_raw1);
    To copy to clipboard, switch view to plain text mode 
    I would like to have a result as following.
    Qt Code:
    1. QByteArray tmp_byte;
    2. tmp_byte.resize(2);
    3. tmp_byte[0]=0x01;
    4. tmp_byte[1]=0x8a;
    To copy to clipboard, switch view to plain text mode 
    Could anyone please help me.
    Thank you very very much.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QString convert to QByteArray

    Are those hex bytes you're trying to add to a QString, only to then put into a QByteArray supposed to represent a particular text encoding? QString internally stores text encoded as UTF-16.

    Why not just append the hex bytes directly to a QByteArray as follows:
    Qt Code:
    1. QByteArray tmp_array;
    2. tmp_array.append(0x01);
    3. tmp_array.append(0x8a);
    To copy to clipboard, switch view to plain text mode 

    Not sure I understand why you're trying to use QString at all here. If you do really have a QString that you want to put into a QByteArray, look at the various QString::to* methods, like QString::toUtf8() for example.
    Last edited by jefftee; 23rd January 2015 at 03:13.

  3. The following user says thank you to jefftee for this useful post:

    schunyeh (28th January 2015)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString convert to QByteArray

    In case you want the numerical values in the string, see QString::toInt(), using a base of 16

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    schunyeh (28th January 2015)

  6. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString convert to QByteArray

    Read about QByteArray::fromHex

  7. The following user says thank you to Lesiok for this useful post:

    schunyeh (28th January 2015)

  8. #5
    Join Date
    Dec 2014
    Posts
    4
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString convert to QByteArray

    Thank jthomps,anda_skoa,Lesiok for the reply.
    The case is that I would like to input a text file that has a decimal number, and then got the numer to change them to the hexadecimal format ( 0xff), & write them to a special binary file format ( 008a 001a .... ..... ).
    After trying to convert, I have found the following API can do it.
    QString test_number; // "0x8a"
    QByteArray test_hex;
    test_hex = QByteArray::fromHex( test_number.toLocal8Bit().constData() );
    qDebug()<<test_hex.at(1); // 0x8a
    Thank you for the help.

Similar Threads

  1. how to convert an int to QByteArray
    By babu198649 in forum Qt Programming
    Replies: 12
    Last Post: 19th September 2014, 10:47
  2. Convert QPixmap to QByteArray ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 13th March 2014, 09:23
  3. Replies: 10
    Last Post: 15th March 2011, 15:14
  4. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 14:33
  5. how can we convert QByteArray to Qstring?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2009, 15:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.