Results 1 to 4 of 4

Thread: Convert from QByteArray to String ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Convert from QByteArray to String ?

    I have this statement:

    [
    QByteArray* messageIn = new QByteArray();
    *messageIn = "Hello\n";
    cout << "Here I should be able to print Hello\n";
    ]

    I need to conver the QByteArray to:
    1. QString
    2. String

    How ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QByteArray to String ?

    QString has a constructor which takes a QByteArray object as a parameter.
    std::string (because I presume this is the subject of the second question) has a constructor that takes a const char* as a parameter and const char * can be obtained from QByteArray using its data() or constData() methods.

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convert from QByteArray to String ?

    I need to be able to print the message contained in the QByteArray in the screen. I am not using any GUI, therefore I am using std::cout.

    std::cout << "The msaage in the QByteArray is: " << here should be the message << "\n";

    How ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QByteArray to String ?

    QByteArray::data() or QByteArray::constData(). Could you please read the docs first before asking such questions? This is elementary knowledge and it surely doesn't belong to "Qt programming" forum. Moving to "Newbie".

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. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 14:33
  4. How to convert from QString to string ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 1st December 2010, 02:50
  5. convert a string to hex
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2009, 11:38

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.