can the type of “FString” be converted to “char*” in ue4 c++?****
Not sure why the docs say it’s returning ‘DataType’. Probably a template thing with FString.
Use the TCHAR_TO_ANSI() macro. You use the * operator on your FString to get it as a TCHAR* and use the macro to cast to char*
char* result = TCHAR_TO_ANSI(*myFString);
GetCharArray() will give you TCHAR* which is wchar_t
it,s working ! thank you!
Is there any limit to the length of ‘myFString’?
When the length of myFString is greater than 127,this approach seems not work well.
Generally, in c++ the only limit on a string’s size would be available memory. That being said, I’m not aware of any limit(s) imposed specifically by FString and I would assume that it should be the same. Keep in mind that the macro shown above is converting to a c-style string using ansi characters and ansi has a very limited range of possible character values. Could that be the issue? If that’s the case and you need to use non-ansi characters then you will need to look at using unicode character encoding.
Edit: Sorry, was a low-on-coffee moment. Size and content shouldn’t matter to the char* variable as it’s potentially raw binary even as far as the compiler is concerned. There must be some other issue not being seen.
Although this is a very old thread, but i stumbled over the problem that i needed a const char* from an FString for a function call. And this approach didn´t work for me and i digged deeper and found some good sources which might be interesting if you stumble over this post:
- TCHAR_TO_ANSI(…) and other Encoding Macros need be used carefully because the result is a pointer to a temporary object and
char* result = TCHAR_TO_ANSI(*myFString);
is a bad idea. These Macro-calls only should be used when they are used as a function parameter.
The sources:
- https://answers.unrealengine.com/que…haracters.html
- https://forums.unrealengine.com/deve…-to-std-string
- https://docs.unrealengine.com/en-US/…ing/index.html
- and which let me think a bit more about this subject: https://www.joelonsoftware.com/2003/…ts-no-excuses/
- to avoid dealing with unsecure temporarely objects we can use
StringCast<ANSICHAR>(*FSTRINGVARIABLE).Get()
to convert a Fstring to a char*
- this means the above approach would be
char* result = StringCast<ANSICHAR>(*myFString).Get();
That gives an error for me:
a value of type “const ANSICHAR*” cannot be used to initialize an entity of type “char*”
This works for me:
char* result = StringCast<ANSICHAR>(*myFString).Get();