Binary array from socket decode to UTF-8

Hi,

I’ve got a bit of C++ which gets data from a socket and creates a string from the binary array - can anyone help me to decode this as UTF8 on the way through?

I’m a bit of a C++ numpty so any help would be great.

I’ve poked around with TCHAR_to_UTF8 and FTCHARToUTF8 but i’m just generating error messages.

Here’s the code which turns the binary into the string. It’s something I originally got Rama to write for me but I thought with a fiddle I should be able to patch it. (And i’m trying to learn C++)

Code:

 FString ARamaTCPReceiver::StringFromBinaryArray(const TArray<uint8>& BinaryArray)
    {
    	//Create a string from a byte array!
    	const std::string cstr( reinterpret_cast<const char*>(BinaryArray.GetData()), BinaryArray.Num() );
    	return FString(cstr.c_str());
    }

As it is i’m just getting question marks on the arabic characters i’m sending in - That’s not just in the console - it’s if I write it to a widget with a fully compatible font.

I tried changing the return line to

Code:

return FString(TCHAR_TO_UTF8(*cstr.c_str()));

It compiles…but it crashes…

Thanks VERY MUCH in advance.

Dan

Hello, Dannington

I am sorry to hear about your problem.

Let’s assume you have FString message, that is serialized like this (in your situation, this is the way message is converted before being sent via socket):

TCHAR *SerializedChar = Message.GetCharArray().GetData();
int32 Size = FCString::Strlen(SerializedChar);
int32 Sent = 0;
uint8* ResultChars = (uint8*)TCHAR_TO_UTF8(SerializedChar);

Then you can do the following:

    const std::string cstr( reinterpret_cast<const char*>(ResultChars), CStringSize);
    int32 NeededSize = MultiByteToWideChar(CP_UTF8, 0, &cstr[0], (int32)cstr.size(), NULL, 0);
    std::wstring wstrTo(NeededSize, 0);
    MultiByteToWideChar(CP_UTF8, 0, &cstr[0], (int32)cstr.size(), &wstrTo[0], NeededSize);
    return FString(wstrTo.c_str());

Please note that this solution is platform-specific. Thus, if you use platform, difeerent from Windows, keep in mind that there are some things that need to be adjusted.

Hope this helped!

Have a wonderful day!

Hi - Thanks very much for this, i’m very excited to implement this.

Thanks again!!!

Dan

Hi,
I’m doing it this way:

FString ARamaTCPReceiver::StringFromBinaryArray(const TArray<uint8>& BinaryArray)
     {
         BinaryArray.Add(0);
	     FString Result = FString(UTF8_TO_TCHAR(BinaryArray.GetData()));
	     return Result;
     }

Its working and a bit cleaner.

Thank you! This works and is so much cleaner than green answer.