How to get fstring size?

I encountered a problem sending the string

this’s my code:

Socket->Send((uint8*)TCHAR_TO_UTF8(*StringValue), StringValue.Len(), SocketSent);

I use FString.Len() to get the length of FString

if “StringValue” is English , this’s normal

but if “StringValue” it’s Chinese , this’s wrong , it’s length is too short

Maybe Chinese accounted for more bytes?

FString is just an array of TCHARs. FString::Len() will return the size of that array. If you see more characters than FString::Len() is reporting then most likely there are two (or more) Chinese characters within the same TCHAR in the FString array.

Yes, I found that sometimes 2 bytes, sometimes 3 bytes, sometimes 4 bytes

I checked, one TCHAR is one Chinese character

Oh. I think it might be because you are converting from TCHAR to byte. Try StringValue.Len() * sizeof(TCHAR)

I am trying

This’s my code:

Socket->Send((uint8*)TCHAR_TO_UTF8(*StringValue), StringValue.Len()*sizeof(TCHAR), SocketSent);

I use the “啊” character to test

The “啊” character originally should account for 3 bytes

But here only calculated 2 bytes

Not sure why it would be three bytes. Try using “FTCHARToUTF8”. I see in the engine source that this is how they convert. Here’s what you should try:

FTCHARToUTF8 Converted(*StringValue);
Socket->Send((uint8*)Converted.Get(), Converted.Length(), SocketSent);

You will need to add #include "StringConv.h" as well

1 Like

I am trying to find a solution

I’m trying

cool ~ , it work, thank you :slight_smile:

Cool indeed. You’re welcome.

:slight_smile:

I have been looking for this solution for hours. Thanks a lot!