Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString to ASCII QByteArray
Forum Updated to NodeBB v4.3 + New Features

QString to ASCII QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
qstring
4 Posts 3 Posters 23.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #1

    Hello,
    I need to convert a QString data into an ASCII containing QByteArray (I need to have the ^Z character in it). In Qt 4 there was the QString::toAscii method however it was deprecated in favor of QString::toLatin1. There's note put in the documentation about said method, though:

    The returned byte array is undefined if the string contains non-Latin1 characters. Those characters may be suppressed or replaced with a question mark.

    As far as I know, the latin1 charset (ISO/IEC 8859-1:1998) doesn't specify anything about the codes for non-printable characters under 0x20. So my question boils down to:
    Can I use QString::toLatin1, and will my ^Z character be preserved?

    Kind regards.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      @kshegunov said:

      I need to convert a QString data into an ASCII containing QByteArray (I need to have the ^Z character in it).

      Just an idea: why not use QString::toUtf8? Since all UTF-8 characters less than 128 are the same as ASCII, then that should give you your ^Z character correctly.

      If your string happens to contain characters greater than 127, then what would you expect to get in the QByteArray? Latin-1? UTF-8? Something else? Where is the QByteArray going / being used?

      Depending on your answers, its possible that what you really want is one or more of:

      • QString::toLocal8Bit
      • QString::toUtf8
      • not use QString at all, but start and end with QByteArray (if you're dealing with binary data).

      In Qt 4 there was the QString::toAscii method however it was deprecated in favor of QString::toLatin1.

      For what its worth, here's what source code for QString::toAscii looked like in Qt4:

      QByteArray QString::toAscii() const
      {
      #ifndef QT_NO_TEXTCODEC
          if (codecForCStrings)
              return codecForCStrings->fromUnicode(*this);
      #endif // QT_NO_TEXTCODEC
          return toLatin1();
      }
      

      So you were probably getting either equivalent behaviour to QString::toLocal8Bit, or QString::toLatin1? No wonder the function was deprecated... its behaviour is pretty ambiguous, and host-configuration dependant (when you look into the default text codec).

      Cheers.

      kshegunovK 1 Reply Last reply
      1
      • Paul ColbyP Paul Colby

        @kshegunov said:

        I need to convert a QString data into an ASCII containing QByteArray (I need to have the ^Z character in it).

        Just an idea: why not use QString::toUtf8? Since all UTF-8 characters less than 128 are the same as ASCII, then that should give you your ^Z character correctly.

        If your string happens to contain characters greater than 127, then what would you expect to get in the QByteArray? Latin-1? UTF-8? Something else? Where is the QByteArray going / being used?

        Depending on your answers, its possible that what you really want is one or more of:

        • QString::toLocal8Bit
        • QString::toUtf8
        • not use QString at all, but start and end with QByteArray (if you're dealing with binary data).

        In Qt 4 there was the QString::toAscii method however it was deprecated in favor of QString::toLatin1.

        For what its worth, here's what source code for QString::toAscii looked like in Qt4:

        QByteArray QString::toAscii() const
        {
        #ifndef QT_NO_TEXTCODEC
            if (codecForCStrings)
                return codecForCStrings->fromUnicode(*this);
        #endif // QT_NO_TEXTCODEC
            return toLatin1();
        }
        

        So you were probably getting either equivalent behaviour to QString::toLocal8Bit, or QString::toLatin1? No wonder the function was deprecated... its behaviour is pretty ambiguous, and host-configuration dependant (when you look into the default text codec).

        Cheers.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Paul-Colby
        That was fast! ;)

        Just an idea: why not use QString::toUtf8? Since all UTF-8 characters less than 128 are the same as ASCII, then that should give you your ^Z character correctly.

        Works for me, I don't know why I didn't think of that in the first place ... :D

        If your string happens to contain characters greater than 127

        It doesn't.

        Something else? Where is the QByteArray going / being used?

        It's going through the serial port, but I am dealing with text data. (QSerialPort expects a QByteArray, and the device accepts ASCII data, possibly other charsets but I don't want to deal with them ...)

        No wonder the function was deprecated... its behaviour is pretty ambiguous, and host-configuration dependant

        QString::toLocal8Bit is host-configuration dependent as well, but as I said I'm dealing with latin text only + a few non-printables (under 0x20).

        Thanks for the suggestion(s)! I'll stick to utf8 and everything should be just fine.
        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • andrA Offline
          andrA Offline
          andr
          wrote on last edited by
          #4

          If .toAscii() worked for you before, you can savely use .toLatin1(), too, and ^Z will be preserved.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved