Results 1 to 3 of 3

Thread: Convert Qbytearray data to ascii

  1. #1
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Convert Qbytearray data to ascii

    Hey i have qbytearray data and i want them to be shown to a Qtextedit widget. Right now i get a line of numbers which i need to convert to ascii.
    I had the problem this guy describes with the "?". I used his example but i get an error i think it's because of the Qtextedit.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert Qbytearray data to ascii

    To convert QByteArray to ASCII, you should do this, as explained in the StackOverflow post:

    Qt Code:
    1. std::string stdString(byteArray.constData(), byteArray.length());
    To copy to clipboard, switch view to plain text mode 

    If you need a QString, then you can use QString::fromStdString(). If you see non-ASCII characters, it is probably because your byte array contains unicode characters. In this case, you may have to use std::wstring instead of std::string and QString::fromStdWString().

    Of course, if your byte array doesn't contain ASCII or unicode characters but is just a binary array, converting it to a string won't work no matter how you do it.

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

    Qg90 (19th August 2014)

  4. #3
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Convert Qbytearray data to ascii

    Hey thank you this is how it worked.

    Qt Code:
    1. std::string message(usb_data.constData(), usb_data.length());
    2. QString qmessage = QString::fromStdString(message);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how to convert an int to QByteArray
    By babu198649 in forum Qt Programming
    Replies: 12
    Last Post: 19th September 2014, 09:47
  2. How to convert QByteArray to char*?
    By Gokulnathvc in forum Newbie
    Replies: 11
    Last Post: 23rd January 2014, 07:34
  3. Convert a QByteArray to a QVariantList
    By franco.amato in forum Newbie
    Replies: 21
    Last Post: 13th May 2010, 00:17
  4. get ASCII equivalent data
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2009, 04:13
  5. convert ebcdic to ascii
    By jaca in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2008, 20:49

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.