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. Can not assign QString in to LPCSTR
Forum Updated to NodeBB v4.3 + New Features

Can not assign QString in to LPCSTR

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.9k Views 1 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.
  • EngelardE Offline
    EngelardE Offline
    Engelard
    wrote on last edited by
    #5

    Well, 1st - there is no converstion toUtf16, only utf16 function which just don't allowed to do such stuff:

    alt text

    @aha_1980 said in Can not assign QString in to LPCSTR:

    Is the UNICODE macro defined?

    No. What is that and where it should be placed and what it change in my case?

    aha_1980A JonBJ 2 Replies Last reply
    0
    • EngelardE Engelard

      Well, 1st - there is no converstion toUtf16, only utf16 function which just don't allowed to do such stuff:

      alt text

      @aha_1980 said in Can not assign QString in to LPCSTR:

      Is the UNICODE macro defined?

      No. What is that and where it should be placed and what it change in my case?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Engelard

      Just see here: https://docs.microsoft.com/en-us/windows/desktop/intl/conventions-for-function-prototypes

      It seems for you it is not defined, so you are using the ANSI Windows API - that's why utf16() is not compatible to LPCSTR. Try my second recommendation from my last post.

      Qt has to stay free or it will die.

      1 Reply Last reply
      2
      • EngelardE Engelard

        Well, 1st - there is no converstion toUtf16, only utf16 function which just don't allowed to do such stuff:

        alt text

        @aha_1980 said in Can not assign QString in to LPCSTR:

        Is the UNICODE macro defined?

        No. What is that and where it should be placed and what it change in my case?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #7

        @Engelard
        Really you were supposed to copy from

        the answer from @kshegunov here: https://forum.qt.io/topic/93580/can-t-convert-from-qstring-to-lpctstr/3

        so

        #ifdef UNICODE
        LPCWSTR something = someString.utf16();
        #else
        QByteArray latinString = someString.toLatin1();
        LPCSTR something = latinString.constData();
        #endif
        

        That would work regardless of whether UNICODE is or is not defined. In your case it sounds like it will resolve to the #else segment.

        As @aha_1980 says, this code will make the lifetime of the LPCSTR result (something) last as long as the QByteArray latinString is still in-scope.

        After this, if you still claim:

        It is not crash program and gives result, but that result is quite wrong.

        then how are you looking at the result, what makes you think it is wrong?

        EngelardE 1 Reply Last reply
        2
        • JonBJ JonB

          @Engelard
          Really you were supposed to copy from

          the answer from @kshegunov here: https://forum.qt.io/topic/93580/can-t-convert-from-qstring-to-lpctstr/3

          so

          #ifdef UNICODE
          LPCWSTR something = someString.utf16();
          #else
          QByteArray latinString = someString.toLatin1();
          LPCSTR something = latinString.constData();
          #endif
          

          That would work regardless of whether UNICODE is or is not defined. In your case it sounds like it will resolve to the #else segment.

          As @aha_1980 says, this code will make the lifetime of the LPCSTR result (something) last as long as the QByteArray latinString is still in-scope.

          After this, if you still claim:

          It is not crash program and gives result, but that result is quite wrong.

          then how are you looking at the result, what makes you think it is wrong?

          EngelardE Offline
          EngelardE Offline
          Engelard
          wrote on last edited by Engelard
          #8

          @JonB said in Can not assign QString in to LPCSTR:

          then how are you looking at the result, what makes you think it is wrong?

          Take a look at my second post in this topic. If i'll assign directly my

          LPCSTR var = "\x64";
          

          Function gives the correct result

          @aha_1980 said in Can not assign QString in to LPCSTR:

          Try my second recommendation from my last post.

          Tried. Same as .toLatin1(), wrong result but compiles and worked.

          @JonB said in Can not assign QString in to LPCSTR:

          That would work regardless of whether UNICODE is or is not defined.

          Just tried throw .constData(), same wrong result.

          Guys. I just thought that problem might be in the 'thing' i'm assign, because such string it is not eventually a string, it is a byte "\x64" it is single stuff which stays for char №100 or 'd'. But i have to assign it only such way "\x64" to my LPCSTR...

          1 Reply Last reply
          0
          • EngelardE Offline
            EngelardE Offline
            Engelard
            wrote on last edited by
            #9

            Now i understood. Need assign not QString into LPCSTR, but to something between. Like:

            QString -> SomeProperType for such kind of byte -> LPCSTR.

            Because when assignment is like "\x64\x00\x5f\x0c" it's actually don't assign string with 16 characters, it's 4 chars with some special type.

            aha_1980A 1 Reply Last reply
            0
            • EngelardE Engelard

              Now i understood. Need assign not QString into LPCSTR, but to something between. Like:

              QString -> SomeProperType for such kind of byte -> LPCSTR.

              Because when assignment is like "\x64\x00\x5f\x0c" it's actually don't assign string with 16 characters, it's 4 chars with some special type.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @Engelard

              as I said earlier, we need more infos. what do you wanne do with the LPCSTR?

              My crystal ball is out for repair, therefore I cannot help you more.

              Best would be a minimal, complete example of your problem.

              Regards

              Qt has to stay free or it will die.

              EngelardE 1 Reply Last reply
              1
              • aha_1980A aha_1980

                @Engelard

                as I said earlier, we need more infos. what do you wanne do with the LPCSTR?

                My crystal ball is out for repair, therefore I cannot help you more.

                Best would be a minimal, complete example of your problem.

                Regards

                EngelardE Offline
                EngelardE Offline
                Engelard
                wrote on last edited by Engelard
                #11

                @aha_1980 I definitely can't give example of code because program is kinda huge, but i know for 100% in which place problem is, and that part i posted here multiple times, in very first post everything you need (in that two versions of code example).

                For example i need to assign to my LPCSTR byte (only one byte for beginning) which have value of 100, if it will be char it's 'd', and if it will have form of signature, that would be "\x64" where 64 is 100 in hex. All next 3 ways of assignment have same functionality:

                • LPCSTR var = "d";

                • LPCSTR var = "\x64";

                • char tempChar[] = "d";
                  LPCSTR var = tempChar;

                All those 3 are working perfectly and functions which using that LPCSTRs are giving correct result. Tried with std::string. It's not working at all, crashing program if i'll do it like that:

                std::string str = "d";
                LPCSTR var = str.c_str();
                

                But i rly can't understand why. Why string with "d" different from that array tempChar[]. But it is.

                1 Reply Last reply
                0
                • EngelardE Offline
                  EngelardE Offline
                  Engelard
                  wrote on last edited by Engelard
                  #12

                  Damn i resolved it....
                  By putting that new example of assignment not from QString but from default char, gives me and hint for a new approach. Char is exactly that conversion in between about which i was talking about it earlier in this post.

                  Here is the way i found it finally working:

                  QString tempStr = "\x64";      */ or might be "d" /*
                  int length = tempStr.length();
                  
                  char *chr = new char[length+1];
                  strcpy(chr, tempStr.toLatin1());
                  LPCSTR var = chr;
                  
                  1 Reply Last reply
                  0
                  • kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #13

                    Hm, this thread is a little bit amusing, as it pretty much mirrors the older one.

                    Read and abide by the Qt Code of Conduct

                    EngelardE 1 Reply Last reply
                    2
                    • kshegunovK kshegunov

                      Hm, this thread is a little bit amusing, as it pretty much mirrors the older one.

                      EngelardE Offline
                      EngelardE Offline
                      Engelard
                      wrote on last edited by Engelard
                      #14

                      @kshegunov Well, people often ask for same thing, and getting same answers from me over and over.

                      1 Reply Last reply
                      0
                      • BuckwheatB Offline
                        BuckwheatB Offline
                        Buckwheat
                        wrote on last edited by
                        #15

                        @Engelard said in Can not assign QString in to LPCSTR:

                        y

                        @Engelard
                        Can I as why you wish to even put into a LPCSTR? You are not planning on changing the data... correct? You can pass your QByteArray as an argument to any function taking an LPCSTR directly.

                        Also from your earlier post with the different stack allocation methods. Any array that worked on the stack after you exit your routine is just luck. Plain dumb luck. The std::string fails because it internally allocates memory on the heap which is destroyed when the std::string's destructor is called leaving the function. The LPCSTR and char methods are all allocated on the stack and if the stack is not corrupted then they still... sort of... exist.

                        Dave Fileccia

                        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