Download

how to convert "int32" to "string" in ue4 c++?

how to convert “int32” to “string” in ue4 c++?

A quick look in the FString documentation reveals the AppendInt() function: FString::AppendInt | Unreal Engine Documentation

Wiki For You

I have a wiki on exactly this subject!

I cover

**FString to uint8 / int32

FString to float

float to FString

int32 to FString

FString to std::string

std:string to FString**


**Wiki Link**

**Rama's Wiki on String Conversions**
https://wiki.unrealengine.com/String_Conversions:_FString_to_FName,_FString_to_Int32,_Float_to_FString

C++ Code For You

This is the code you want:



int32 YourInt = 9000;
FString IntAsString = FString::FromInt(YourInt);


Enjoy!

Rama

2 Likes

FString::FromInt( YourInt32Here);

But please look at Rama’s wiki thread too. Lots of amazing and good information there. :slight_smile:

Hi!

I hope you already solved your issue since it has been a while.
However, I post my code here in case it can help anyone else since I recently had the same issue.
Here is one way of how you can parse an integer from a string in a safe way! No undefined behaviors.



// header file
class StringToInt
{
public:

    static int32 convert(const FString &string, int32 defaultValue = 0);
};

// source file
int32 StringToInt::convert(const FString &string, int32 defaultValue)
{
    if (string.IsEmpty()
        || string[0] == L'-' && string.Len() == 1)
    {
        return defaultValue;
    }

    int32 value = 0;

    int32 base = 1;
    for (int32 i = string.Len() - 1; i >= 0; --i)
    {
        auto character = string*;

        switch (character)
        {
        case L'0':
            break;
        case L'1':
            value += base * 1;
            break;
        case L'2':
            value += base * 2;
            break;
        case L'3':
            value += base * 3;
            break;
        case L'4':
            value += base * 4;
            break;
        case L'5':
            value += base * 5;
            break;
        case L'6':
            value += base * 6;
            break;
        case L'7':
            value += base * 7;
            break;
        case L'8':
            value += base * 8;
            break;
        case L'9':
            value += base * 9;
            break;

        case L'-':
            if (i == 0)
            {
                return -value;
            }
            else
            {
                return defaultValue;
            }
            break;

        default:
            return defaultValue;
            break;
        }

        base *= 10;
    }

    return value;
}


Old question, but I wanted to add something.

There’s also FString::Printf which works like the classic sprintf from the standard library, use %d for integers.


FString toString = FString::Printf(TEXT("%d"), integer);

https://docs.unrealengine.com/en-US/…tput%20object.