x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Convert FString to std::string

I am trying to use a JSON library in my code. I am still grasping very basics as I am total rookie with C++. I have a trouble of converting FString to std::string. I did found this wiki page, but for some reason I am getting bunch of japanese(?) characters as the result.

I've made this function which is correctly recognized by the json library to do the conversion. However I am sensing there is something not quiet right about it.

 void to_json(json& j, const FString& value) {
     j = TCHAR_TO_UTF8(*value);
 }

Here is the GitHub issue where I have been discussing this. Any idea what might be wrong please?

Edit

Actually I tried this without interference of the JSON lib and it behaves same way.

 FString test = TEXT("MyTest");
 std::string test2 = std::string(TCHAR_TO_UTF8(*test));
 UE_LOG(LogTemp, Warning, TEXT("JSON %s"), test2.c_str());

Would produce following log statement: LogTemp:Warning: JSON 祍敔瑳

Product Version: UE 4.15
Tags:
more ▼

asked Mar 11 '17 at 10:25 AM in C++ Programming

avatar image

Daniel K.
165 9 21 23

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

The problem with your second example is that TEXT("JSON %s") doesn't expect what you fed it. Try giving it a pointer to an FString like this:

  FString test = "MyTest";
  std::string test2 = std::string(TCHAR_TO_UTF8(*test));
  UE_LOG(LogTemp, Warning, TEXT("JSON %s"), *FString(test2.c_str()));

Also, there is no need for the first TEXT() macro.

more ▼

answered Jun 21 '17 at 03:37 PM

avatar image

Graphics_Dev
26 3 4

avatar image Daniel K. Jun 22 '17 at 05:57 AM

Thanks for your answer, although a little bit late as I've given up and bought JSON plugin for UE which works great. This might come in handy later perhaps.

avatar image Cucharón Dec 26 '18 at 10:05 PM

This is nice... if you want to destroy any character different from a-z... To preserve the characters to revert from string to fstring ----> FString(UTF8_TO_TCHAR(test2.c_str()))

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question