×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    30

    Problem in converting QByteArray to Qstring

    QByteArray bytes = /*at start it having non ascii charcter */
    QString string(bytes);
    as a result i got empty string. what should i do now.
    i used bytes to write bytes to write in text file there i find this ( Info OA new version with) when i remove these starting characters form bytes then above conversion works fine but i want to now what should i do to handle this as if i dont now where these non ascii characters are in bytes.

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    Re: Problem in converting QByteArray to Qstring

    See the following example, I copied code example.
    Also see the Qt doc for QString.

    QByteArray payloaddata = record.payload();
    QByteArray btadd = payloaddata.mid(2, 6);
    QByteArray reverseaddress;
    for(int i = 5; i>= 0; i--)
    {
    reverseaddress.append(btadd[i]);
    }
    ui->btaddress->setText(reverseaddress.toHex());

    QByteArray btname = payloaddata.mid(10, payloaddata[8]-1);
    //btname is byte array and we make a QString from it
    QString myString(btname);
    ui->btname->setText(myString);

  3. #3
    Super Contributor
    Join Date
    Mar 2009
    Posts
    1,024

    Re: Problem in converting QByteArray to Qstring

    Hi,
    if you get an empty string you have a '\0' char at beginning.
    If you have a NULL char, the only option is to remove it using a QString::remove.
    If that's not a NULL char the following static functions are what you need.
    You have to chose those according to the codec used to encode the text.

    Usually one of those function is enough to convert data to string:
    QString fromAscii ( const char * str, int size = -1 )
    QString fromLatin1 ( const char * str, int size = -1 )
    QString fromLocal8Bit ( const char * str, int size = -1 )
    QString fromRawData ( const QChar * unicode, int size )
    QString fromStdString ( const std::string & str )
    QString fromStdWString ( const std::wstring & str )
    QString fromUcs4 ( const uint * unicode, int size = -1 )
    QString fromUtf8 ( const char * str, int size = -1 )
    QString fromUtf16 ( const ushort * unicode, int size = -1 )
    QString fromWCharArray ( const wchar_t * string, int size = -1 )

Similar Threads

  1. Replies: 1
    Last Post: 2011-11-13, 12:10
  2. Problem with & in QByteArray ?
    By narendar_discover in forum Qt
    Replies: 5
    Last Post: 2011-05-20, 12:30
  3. Converting qreal to qstring
    By Nujabrn in forum Qt
    Replies: 6
    Last Post: 2010-12-15, 12:21
  4. exception occurs when converting QString to std::string
    By kimitake in forum [Archived] Qt SDKs and Tools
    Replies: 4
    Last Post: 2010-04-29, 17:08

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×