Search code examples
c++unreal-engine4

How do you convert an FString of delimited floats to a TArray<uint16> using Unreal C++


Given an FString of "123.2222,446.4444,55234.2342" how do you convert this to a TArray of type uint16?

Current attempt is to parse the string into an array using

TArray<FString> Parsed;
    HeightMapData.ParseIntoArray(Parsed, TEXT(","), false);

This seems to work well. Next is the problem of how do I convert this to uint16.

I am trying

const TArray<uint16*>& parsedArray = reinterpret_cast<const TArray<uint16*>&>(Parsed);

But it is not working. Error: Parent address is invalid for parsedArray


Solution

  • You can't reinterpret_cast and pretend your array of strings is an array of integers. You need to convert each string to an integer in a new array:

    TArray<uint16> intArray;
    for (const auto& str : Parsed)
        intArray.Add(FCString::Atoi(str));
    

    Ref: https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/StringHandling/FString/