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. Unwanted and annoying backslash in string read from file!

Unwanted and annoying backslash in string read from file!

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprocessqstringqtextcodecunicode
6 Posts 3 Posters 463 Views
  • 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.
  • M Offline
    M Offline
    Mohammadsm
    wrote on 29 Oct 2024, 09:28 last edited by Mohammadsm
    #1

    I want to read a unicode string from a file. file content is this:
    \u0048\u0065\u006c\u006c\u006f

    For reasons in my program, I have to use system calls to read the file. So I've done it through QProcess:

    execute->start("cat /home/user/x");
    execute->waitForFinished(1000);
    qDebug()<<execute->readAllStandardOutput();
    

    And the output is: "\\u0048\\u0065\\u006c\\u006c\\u006f". And it can't be decoded to "Hello" .
    But when I enter it manually, it's fine!

    qDebug()<<"\u0048\u0065\u006c\u006c\u006f";
    

    The output is: "Hello".

    How should I overcome this problem?

    Thanks in advance

    J 1 Reply Last reply 29 Oct 2024, 09:51
    0
    • M Mohammadsm
      29 Oct 2024, 09:28

      I want to read a unicode string from a file. file content is this:
      \u0048\u0065\u006c\u006c\u006f

      For reasons in my program, I have to use system calls to read the file. So I've done it through QProcess:

      execute->start("cat /home/user/x");
      execute->waitForFinished(1000);
      qDebug()<<execute->readAllStandardOutput();
      

      And the output is: "\\u0048\\u0065\\u006c\\u006c\\u006f". And it can't be decoded to "Hello" .
      But when I enter it manually, it's fine!

      qDebug()<<"\u0048\u0065\u006c\u006c\u006f";
      

      The output is: "Hello".

      How should I overcome this problem?

      Thanks in advance

      J Offline
      J Offline
      JonB
      wrote on 29 Oct 2024, 09:51 last edited by
      #2

      @Mohammadsm said in Unwanted and annoying backslash in string read from file!:

      For reasons in my program, I have to use system calls to read the file. So I've done it through QProcess:

      FWIW this makes no sense at all. Running a cat process and reading all standard output in order to read a file is just a crazy inefficient equivalent of using QFile to read its content. I cannot imagine any circumstance where you would need to do this.... But I'll leave it at that.

      I want to read a unicode string from a file. file content is this:
      \u0048\u0065\u006c\u006c\u006f

      How did you get see that string? Do you mean the file length is 5 bytes or do you mean it actually has the string you show (~30 bytes) in it?

      Next possibility:

      qDebug()<<execute->readAllStandardOutput();
      

      readAllStandardOutput() returns a QByteArray for qDebug() to output.

      qDebug()<<"\u0048\u0065\u006c\u006c\u006f";
      

      That is a string/char[]/QString for qDebug() to print.

      qDebug() may well display a QByteArray as an array of bytes but a string as a string, hence only the second one will be shown as a string "Hello". Don't rely on qDebug() as an informative way to display content, it's intended to be human-readable only.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mohammadsm
        wrote on 29 Oct 2024, 14:12 last edited by
        #3

        Thank you @JonB

        Do you mean the file length is 5 bytes or do you mean it actually has the string you show (~30 bytes) in it?

        It's 30 bytes, just like it is.

        \\u0048\\u0065\\u006c\\u006c\\u006f

        I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?

        Don't rely on qDebug() as an informative way to display content, it's intended to be human-readable only.

        I tried with QTextCodec::fromUnicode(const QString &str) but it failed due to this additional backslash.

        J J 2 Replies Last reply 29 Oct 2024, 14:47
        0
        • M Mohammadsm
          29 Oct 2024, 14:12

          Thank you @JonB

          Do you mean the file length is 5 bytes or do you mean it actually has the string you show (~30 bytes) in it?

          It's 30 bytes, just like it is.

          \\u0048\\u0065\\u006c\\u006c\\u006f

          I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?

          Don't rely on qDebug() as an informative way to display content, it's intended to be human-readable only.

          I tried with QTextCodec::fromUnicode(const QString &str) but it failed due to this additional backslash.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 29 Oct 2024, 14:47 last edited by
          #4

          @Mohammadsm said in Unwanted and annoying backslash in string read from file!:

          I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?

          I guess because you used qDebug to print it...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply 9 Nov 2024, 06:45
          0
          • M Mohammadsm
            29 Oct 2024, 14:12

            Thank you @JonB

            Do you mean the file length is 5 bytes or do you mean it actually has the string you show (~30 bytes) in it?

            It's 30 bytes, just like it is.

            \\u0048\\u0065\\u006c\\u006c\\u006f

            I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?

            Don't rely on qDebug() as an informative way to display content, it's intended to be human-readable only.

            I tried with QTextCodec::fromUnicode(const QString &str) but it failed due to this additional backslash.

            J Offline
            J Offline
            JonB
            wrote on 29 Oct 2024, 14:53 last edited by JonB
            #5

            @Mohammadsm said in Unwanted and annoying backslash in string read from file!:

            It's 30 bytes, just like it is.

            Then is not a Unicode string. And your "when I enter it manually as qDebug()<<"\u0048\u0065\u006c\u006c\u006f";" is totally different. Rather the file content is a "human visual representation of the 5 Unicode characters forming the C++ literal string". I do not know of any C++ or Qt function which would convert between the two, you would have to write one yourself. And just to complicate things further, your original was \u0048\u0065\u006c\u006c\u006f, which is 30 bytes, but now you say the file contains \\u0048\\u0065\\u006c\\u006c\\u006f which is 35 bytes and different again....

            I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?

            readStandardOutput() does not return/put in any "extra backslashes" (or any other character). I already informed you about the output format of qDebug(), which does not show precisely, one-for-one what characters you are looking at.

            I tried with QTextCodec::fromUnicode(const QString &str) but it failed due to this additional backslash.

            First you have two posts claiming two different contents for the file, backslash-wise. Second if you really want to get rid of every backslash, or reduce double-backslashes to single-backslash, use something like replace(). And thirdly as I said above if your file does indeed contain either 30 or 35 bytes it is not a "Unicode string" (like your C++ string is) and QTextCodec::fromUnicode() is not going to work on it or produce what you think.

            1 Reply Last reply
            1
            • J jsulm
              29 Oct 2024, 14:47

              @Mohammadsm said in Unwanted and annoying backslash in string read from file!:

              I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?

              I guess because you used qDebug to print it...

              M Offline
              M Offline
              Mohammadsm
              wrote on 9 Nov 2024, 06:45 last edited by
              #6

              @jsulm
              I tried QChar to convert unicode and my problem solved. now qDebug prints the text correctly. 👌🏻
              I also copied the readAllStandardOutput() to a QString and removed backslashes via remove("\x5C\x75").

              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