Can not assign QString in to LPCSTR
-
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.
@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.
-
@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.
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:
- Is the
UNICODE
macro defined? - 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
-
Well, 1st - there is no converstion toUtf16, only utf16 function which just don't allowed to do such stuff:
@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?
-
Well, 1st - there is no converstion toUtf16, only utf16 function which just don't allowed to do such stuff:
@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?
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 toLPCSTR
. Try my second recommendation from my last post. -
Well, 1st - there is no converstion toUtf16, only utf16 function which just don't allowed to do such stuff:
@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?
@Engelard
Really you were supposed to copy fromthe 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 theQByteArray 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?
-
@Engelard
Really you were supposed to copy fromthe 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 theQByteArray 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?
@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...
-
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.
-
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.
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
-
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
@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.
-
-
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;
-
Hm, this thread is a little bit amusing, as it pretty much mirrors the older one.
-
@kshegunov Well, people often ask for same thing, and getting same answers from me over and over.
-
@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.