How to convert uint8 array to int?

I’m writing socket to receive data code

This my code:

	TArray<uint8> ReceiveData;
	uint8 element = 0;
	while (true)
	{
		ReceiveData.Init(element, 1024u);
		int32 read = 0;
		Socket->Recv(ReceiveData.GetData(), ReceiveData.Num(), read);
	}

I can receive data ,

I’m trying to receive an int data,

how to convert uint8 array to int?

Do you receiving int32 data or you want to convert bytes to int32? note that uint8 is blueprint/property editor compatible as Byte type so you can actually use it without any conversion

I will try

I want to send the data I received to the blueprint

My message format looks like this:

int PlayerID, string PlayerName

I received uint8

I am looking for a way to turn it into the original type

Maybe 1 int is equal to 4 uint8?

I just found out that uint8 is unsigned char

This is my code, but it’s still wrong:

            TArray<uint8> ReceiveData;
	uint8 element = 0;
	TArray<uint8> Buffer;

	while (true)
	{
		ReceiveData.Init(element, 4);
		int32 read = 0;
		Socket->Recv(ReceiveData.GetData(), ReceiveData.Num(), read);
		int test_int = static_cast<int>(*(ReceiveData.GetData()));
		NetworkMgr->RecvData(test_int);
	}

I’m trying to modify it…

I want to convert bytes to int

This is my code:

	uint8 recvBuffer[4];
	uint8 element = 0;

	while (true)
	{
		int32 read = 0;
		Socket->Recv(recvBuffer, 4, read, ESocketReceiveFlags::WaitAll);
		int test_int = (int) *recvBuffer;
		NetworkMgr->RecvData(test_int);
	}

It looks like it’s working,
But it still can not pass a negative number

I sent this data from the server:

int PackageLength (4 bit) , int ProtoID (4 bit) , int LoginResult (4 bit) , string LoginKey (32 bit)

The value of the server is:

PackageLength = 44 , ProtoID = 1 , LoginResult = -1 , LoginKey ="\x00\x00\x00\x00…"

My ue4 client received data is: (Currently only consider the int type)

PackageLength = 44 , ProtoID = 1 , LoginResult = 255 , …

This is my solution:

int ABaseNetworkMgr::GetIntByBuffer(TArray<uint8> Buffer, int Offset)
{
	int int_value;
	unsigned char char_list[4];
	for (int i = 0; i < 4; i++)
	{
		char_list[i] = Buffer.GetData()[i+ Offset];
	}
	memcpy(&int_value, char_list, 4);
	return int_value;
}

But I did not take into account the case of Big-Endian and Little-Endian of.

And in cross-platform, memory allocation may be a problem.

1 Like

Cross-platform seems to be able to use FMemory :: Memcpy ()