Need help converting FString to wchar_t

This is probably a beginner problem as I am relatively new to C++.

Short version: I need to convert an FString to a wchar_t. I don’t know how to do this and was hoping someone here could come to the rescue.

The longer version: Now that code plugins are on the Marketplace, I figured this was a good time to toy around with the LCD display on my Logitech G19. Unfortunately for me, the Logitech SDK plugin does not have its functionality exposed to blueprints.

Having created a few basic blueprint nodes earlier I decided to try my hand at this. I’ve now stumbled upon an issue. Many of the functions provided by the plugin takes wchar_t parameters. Blueprints can’t handle this natively, so I figured the best option here is to put a FString input in the blueprint input. In the C++ function for the node I’d convert the FString to a wchar_t and pass it along to the Logitech SDK functions.

As mentioned before I am still fairly new to C++ programming, and don’t have the slightest clue on how to go about achieving this.

FString MyFooString = TEXT(“foofoo”);

TCHAR* tchar_is_wchar_t_under_the_hood = *MyFooString;

Track typedefs:

typedef FPlatformTypes::TCHAR TCHAR; ///< Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requirements of the licensee.
typedef WIDECHAR TCHAR; // A switchable character - In-memory only. Either ANSICHAR or WIDECHAR, depending on a licensee’s requirements.
typedef wchar_t WIDECHAR; // A wide character - In-memory only. ?-bit fixed-width representation of the platform’s natural wide character set. Could be different sizes on different platforms.

1 Like

What Pierdek said. Also you may want to bookmark this page (and the other String pages on the Wiki). It’s great for quick reference.

Thanks for both of your replies, I very much appreciate it! I’ve actually been looking at that very page @ExtraLifeMatt linked, as well as searching both the forums and google. Perhaps this just flies over my head because I’m still fairly fresh, but… woosh! Honestly, the part about tracking typedefs that @Pierdek wrote I only partially understand.

I’m still stuck though. If I post a few of my attempts at the code, perhaps someone could guide me along a bit more. As much as I hate to admit it, I might need some spoon-feeding on this issue.
[HR][/HR]Attempted


bool UK2_LogitechLCD::K2_LogiLcdInit(FString friendlyName, int32 lcdType)
{
	return ILogitechG::LcdInit(*friendlyName, lcdType);
}

Error message:

[HR][/HR]Attempted


bool UK2_LogitechLCD::K2_LogiLcdInit(FString friendlyName, int32 lcdType)
{
	TCHAR* convertedFriendlyName = *friendlyName;
	return ILogitechG::LcdInit(convertedFriendlyName, lcdType);
}

Error message:

Those errors are complaining about trying to pass in a const variable when it expects non-const. I’m curious why the Logitech API asks for a non-const TCHAR array (that implies it plans to write to it, if it doesn’t then that seems like a bug in the API).

There isn’t a way to access a non-const pointer to the internal data of FString, so you’ll have to do the unmentionable and use const_cast:



bool UK2_LogitechLCD::K2_LogiLcdInit(FString friendlyName, int32 lcdType)
{
	return ILogitechG::LcdInit(const_cast<TCHAR*>(*friendlyName), lcdType);
}


Just repent afterwards.