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. Solved : Reading ASCII/UTF-8 file
QtWS25 Last Chance

Solved : Reading ASCII/UTF-8 file

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 6.2k 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.
  • T Offline
    T Offline
    Thibaut
    wrote on 18 Jul 2012, 13:09 last edited by
    #1

    Hi all,

    I am using Qt to read a file generated by the method store of the Java Properties class. This method generates an ASCII file. However, special characters are written in their Utf-8 equivalent. In the end, the ascii file looks like that :

    firstname = g\u00C9rard
    lastname = normand

    Now, when I read and display the data, I would like to display the symbols itself (gérard normand). I tried using the toUtf8() method but nothing convincing came out. So how do I read this ascii file while handling the Utf-8 symbols ?

    Thanks for your help

    Thibaut

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 18 Jul 2012, 13:45 last edited by
      #2

      The format you describe isn't UTF-8, but an escaping sequence. You'll need to parse those. AFAIK, there is no standard Qt codec that deals with these. You could create your own though. See [[doc:QTextCodec]] for more information on that.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jeroentjehome
        wrote on 18 Jul 2012, 14:34 last edited by
        #3

        Hi, the \u00C9 is a utf32 representation of a capital letter é, so getting the small letter is going to be trouble some. Think as Andre mentioned you need to make your own conversion class to handle this one.

        Greetz, Jeroen

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Thibaut
          wrote on 19 Jul 2012, 08:13 last edited by
          #4

          Thanks for the reply. The capital letter é is what I want. I went on http://www.fileformat.info/info/unicode/char/c9/index.htm and it says that \u00C9 is Java/C++ source code for the capital é character. So I was thinking there might be a way around it.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Thibaut
            wrote on 19 Jul 2012, 08:44 last edited by
            #5

            Finally found what I was looking for.
            I need to use the following routine

            @QRegExp rx("(\\u[0-9a-fA-F]{4})");
            int pos = 0;
            while ((pos = rx.indexIn(str, pos)) != -1) {
            str.replace(pos++, 6, QChar(rx.cap(1).right(4).toUShort(0, 16)));
            }@

            Thanks a lot for your replies

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on 19 Jul 2012, 08:45 last edited by
              #6

              Did you check out "fromUnicode?":http://qt-project.org/doc/qt-4.8/qtextcodec.html#fromUnicode
              At least from the name and description it seems to be fitting. However, Andre may have more experience with this.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 19 Jul 2012, 08:54 last edited by
                #7

                [quote author="koahnig" date="1342687524"]Did you check out "fromUnicode?":http://qt-project.org/doc/qt-4.8/qtextcodec.html#fromUnicode
                At least from the name and description it seems to be fitting. However, Andre may have more experience with this. [/quote]

                The problem is that the codec used is not a codec in the normal sense. It is a unicode escape sequence in an otherwise ASCII-encoded file. So to use this method, you first have to actually implement a codec that does that translation back and forth. If you have to work with these files, it is probably a good idea to implement such a codec. Doesn't seem all that hard to me...

                Edit: though, I admit, I did not try doing it myself, so it might be harder than it seems by just looking at the docs...

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on 19 Jul 2012, 09:00 last edited by
                  #8

                  [quote author="Andre" date="1342688046"]
                  [quote author="koahnig" date="1342687524"]Did you check out "fromUnicode?":http://qt-project.org/doc/qt-4.8/qtextcodec.html#fromUnicode
                  At least from the name and description it seems to be fitting. However, Andre may have more experience with this. [/quote]

                  The problem is that the codec used is not a codec in the normal sense. It is a unicode escape sequence in an otherwise ASCII-encoded file. So to use this method, you first have to actually implement a codec that does that translation back and forth. If you have to work with these files, it is probably a good idea to implement such a codec. Doesn't seem all that hard to me...
                  [/quote]

                  Andre, thanks for clarification

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0

                  2/8

                  18 Jul 2012, 13:45

                  topic:navigator.unread, 6
                  • Login

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