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. transforming QString to microsoft's CString, and CString to QString

transforming QString to microsoft's CString, and CString to QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.6k 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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    is there a perfect solution to Conver between QString and CString?
    like this ?
    QString to CString: toLocal8Bit for multibyte encoding, toWString for unicode encoding.
    CString to QString: fromLocal8Bit for multibyte encoding, fromWString for unicode encoding.

    and when to use toLatin1()?
    when to use toUtf8()?

    raven-worxR 1 Reply Last reply
    0
    • O opengpu

      is there a perfect solution to Conver between QString and CString?
      like this ?
      QString to CString: toLocal8Bit for multibyte encoding, toWString for unicode encoding.
      CString to QString: fromLocal8Bit for multibyte encoding, fromWString for unicode encoding.

      and when to use toLatin1()?
      when to use toUtf8()?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @opengpu said in transforming QString to microsoft's CString, and CString to QString:

      and when to use toLatin1()?
      when to use toUtf8()?

      heavily depends on your usecase and what you want to do with the string

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • O Offline
        O Offline
        opengpu
        wrote on last edited by
        #3

        for example?

        jsulmJ 1 Reply Last reply
        0
        • O Offline
          O Offline
          opengpu
          wrote on last edited by
          #4

          now i have a MFC project that use CString. the project is using Unicode is VS propety, but i think i have to take care of Multi-Byte.
          i am changing this MFC project into Qt project.
          so i want to know the right way to convert between QString and CString.
          thanks

          1 Reply Last reply
          0
          • O opengpu

            for example?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @opengpu said in transforming QString to microsoft's CString, and CString to QString:

            for example?

            For example if your string only contains Latin-1 characters you can use toLatin1(). It is described in the documentation: https://doc.qt.io/qt-5/qstring.html#toLatin1

            Using Google I found: https://stackoverflow.com/questions/6215833/how-do-i-convert-an-atl-mfc-cstring-to-a-qstring

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

            1 Reply Last reply
            4
            • aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              IIRC, when UNICODE is defined, the format of CString and QString is both UTF-16, so that should be the easiest format to exchange data.

              https://doc.qt.io/qt-5/qstring.html#fromWCharArray and https://doc.qt.io/qt-5/qstring.html#toWCharArray should work for you.

              You can probably also use https://doc.qt.io/qt-5/qstring.html#utf16 but remember that this will only work with casting and on Windows (where wchar_t is 16 bit).

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              4

              • Login

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