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. LineEdit widget text to QByteArray (\n to 0x0A)

LineEdit widget text to QByteArray (\n to 0x0A)

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 3.6k 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.
  • A Offline
    A Offline
    Andiz
    wrote on last edited by
    #1

    Hello,

    how to conver QString from LineEdit->text() to QByteArray? \n must be converted to byte 0x0A.

    Thank You.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Depends on the encoding you need in the resulting byte array. E.g., to get a UTF-8 encoded string, you'd use QByteArray QString::toUtf8() const.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Andiz
        wrote on last edited by
        #3

        I use
        QByteArray data_to_send;
        data_to_send = ui->lineEdit_send->text().toUtf8();

        and I get two bytes 0x5C 0x6E in byte array when I write in line edit \n
        but I need 0x0A

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          You could e.g. replace the 2-character-sequence of "\n" in the string before you convert it to a byte array:

          data_to_send = QString( ui->lineEdit_send->text() ).replace("\\n", "\n").toUtf8();
          
          1 Reply Last reply
          1
          • A Offline
            A Offline
            Andiz
            wrote on last edited by
            #5

            It works, and what about other syntax like \r CR and etc? its possible to convert all of them to hex correctly?

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Sure, just repeat the code from my last post for all sequences you want to replace. A list of the escape sequences used in C++ can be found here. As a side-note, you could also use QRegularExpression instead of QString::replace(). Or you could replace the characters in the byte array, using QByteArray::replace().

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Andiz
                wrote on last edited by Andiz
                #7

                I tryed to use: data_to_send = ui->lineEdit_send->text().replace( "\\" , "\" ).toUtf8();
                but the end of this line seems to be commented because there is ""

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by A Former User
                  #8

                  .replace( "\" , "" ) is an error in your source code, because \" is the escape sequence for double quote. Have a look at http://en.cppreference.com/w/cpp/language/escape to see how escape sequences in C++ work.

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Wouldn't QByteArray::toHex fill the bill ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Andiz
                      wrote on last edited by
                      #10

                      OK. I used this method:
                      replace("\\'","\'").replace("\\\"", "\"").replace("\\?","\?").replace("\\a","\a").replace("\\b","\b").replace("\\f","\f").replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\v","\v")

                      Thank You

                      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