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

Can not assign QString in to LPCSTR

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.9k 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.
  • E Offline
    E Offline
    Engelard
    wrote on 12 Oct 2018, 22:18 last edited by
    #1

    Program compiles but freezes and then crush in runtime when i use this function.
    Here is part of code which cause it:

    QString tempStr = "\x64";
    LPCSTR var = tempStr.toStdString().c_str();
    

    Btw, if there will be no tempStr and part of code will be like:

    LPCSTR var = "\x64";
    

    Everything works perfect.

    A 1 Reply Last reply 12 Oct 2018, 23:24
    0
    • E Offline
      E Offline
      Engelard
      wrote on 14 Oct 2018, 21:52 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
      • E Engelard
        12 Oct 2018, 22:18

        Program compiles but freezes and then crush in runtime when i use this function.
        Here is part of code which cause it:

        QString tempStr = "\x64";
        LPCSTR var = tempStr.toStdString().c_str();
        

        Btw, if there will be no tempStr and part of code will be like:

        LPCSTR var = "\x64";
        

        Everything works perfect.

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 12 Oct 2018, 23:24 last edited by
        #2

        hi @Engelard,

        you already asked this, and you already got the answer from @kshegunov here: https://forum.qt.io/topic/93580/can-t-convert-from-qstring-to-lpctstr/3

        he also explains why your current approach is wrong.

        Qt has to stay free or it will die.

        E 1 Reply Last reply 13 Oct 2018, 01:00
        3
        • A aha_1980
          12 Oct 2018, 23:24

          hi @Engelard,

          you already asked this, and you already got the answer from @kshegunov here: https://forum.qt.io/topic/93580/can-t-convert-from-qstring-to-lpctstr/3

          he also explains why your current approach is wrong.

          E Offline
          E Offline
          Engelard
          wrote on 13 Oct 2018, 01:00 last edited by
          #3

          @aha_1980 Ah yea....

          Somehow forgot about that topic after it compiles with str.toStdString().c_str();
          But when after couple days i tested that stuff it showed up..
          I don't get completely how i can cast to utf16(as he suggested). But:

          someString.toLatin1();

          worked, but gives wrong result(i can't take a look what string inside my LPCSTR var using Qt with their QStrings only everywhere).

          I simply runs tests, so. When part of code in function like this:

          LPCSTR var = "\x64";
          
          • it's works perfectly, but when it like:
          QString tempStr = "\x64";
          LPCSTR var = tempStr.toLatin1();
          

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

          A 1 Reply Last reply 13 Oct 2018, 09:19
          0
          • E Engelard
            13 Oct 2018, 01:00

            @aha_1980 Ah yea....

            Somehow forgot about that topic after it compiles with str.toStdString().c_str();
            But when after couple days i tested that stuff it showed up..
            I don't get completely how i can cast to utf16(as he suggested). But:

            someString.toLatin1();

            worked, but gives wrong result(i can't take a look what string inside my LPCSTR var using Qt with their QStrings only everywhere).

            I simply runs tests, so. When part of code in function like this:

            LPCSTR var = "\x64";
            
            • it's works perfectly, but when it like:
            QString tempStr = "\x64";
            LPCSTR var = tempStr.toLatin1();
            

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

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 13 Oct 2018, 09:19 last edited by
            #4

            @Engelard

            You will need to give some more information about your problem, especially more context of what you want to do and where things don't work as you expect.

            I don't get completely how i can cast to utf16(as he suggested).

            What's wrong there? Do you have compile errors? Then please post them here. Having a compiler log also helps us to understand which defines are passed.

            someString.toLatin1(); worked, but gives wrong result

            So I guess it did not work ;) Two questions here:

            1. Is the UNICODE macro defined?
            2. You are aware that toLatin1() creates a temporary object? You can try to store it in a QByteArray to extend it's lifetime: const QByteArray ba = someString.toLatin1();

            (i can't take a look what string inside my LPCSTR var)

            Why? That is just a pointer to some memory, you can always have a look there.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            3
            • E Offline
              E Offline
              Engelard
              wrote on 13 Oct 2018, 11:10 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?

              A J 2 Replies Last reply 13 Oct 2018, 11:27
              0
              • E Engelard
                13 Oct 2018, 11:10

                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?

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 13 Oct 2018, 11:27 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
                • E Engelard
                  13 Oct 2018, 11:10

                  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?

                  J Offline
                  J Offline
                  JonB
                  wrote on 13 Oct 2018, 14:35 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?

                  E 1 Reply Last reply 13 Oct 2018, 23:22
                  2
                  • J JonB
                    13 Oct 2018, 14:35

                    @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?

                    E Offline
                    E Offline
                    Engelard
                    wrote on 13 Oct 2018, 23:22 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
                    • E Offline
                      E Offline
                      Engelard
                      wrote on 14 Oct 2018, 03:05 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.

                      A 1 Reply Last reply 14 Oct 2018, 06:09
                      0
                      • E Engelard
                        14 Oct 2018, 03:05

                        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.

                        A Offline
                        A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on 14 Oct 2018, 06:09 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.

                        E 1 Reply Last reply 14 Oct 2018, 21:23
                        1
                        • A aha_1980
                          14 Oct 2018, 06:09

                          @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

                          E Offline
                          E Offline
                          Engelard
                          wrote on 14 Oct 2018, 21:23 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
                          • E Offline
                            E Offline
                            Engelard
                            wrote on 14 Oct 2018, 21:52 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 14 Oct 2018, 22:39 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

                              E 1 Reply Last reply 15 Oct 2018, 03:49
                              2
                              • kshegunovK kshegunov
                                14 Oct 2018, 22:39

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

                                E Offline
                                E Offline
                                Engelard
                                wrote on 15 Oct 2018, 03:49 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 16 Oct 2018, 12:53 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

                                  3/15

                                  13 Oct 2018, 01:00

                                  topic:navigator.unread, 12
                                  • Login

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