Const TArray to string

I am trying to compress an image, convert it to a string, add it to a JSON object (with some other properties) and then send it via TCP.

Here is how I compress the image:

TArray<uint8> CompressedData;

FImageUtils::CompressImageArray(
		1024,
		1024,
		ColorBuffer,
		CompressedData
);
		
	server.Process(CompressedData);

My Process function definition looks like this:

void Process(const TArray<uint8> data);

Now, since I need to send JSON (using rapidjson), I need to convert it to a string.
I have tried:

FString frameAsFString =  StringFromBinaryArray(data);
string frameAsString(TCHAR_TO_UTF8(*frameAsFString));

Where StringFromBinaryArray is from Rama:

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

	//FString can take in the c_str() of a std::string
	return FString(cstr.c_str());
}

But the frameAsString looks like this: “?PNG\r\n\u001A\n”

I then tried:

string s(*data.GetData(), sizeof(data));

But from that, frameAsString looks like this:

\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u00
10\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u
0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010
\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u00
10\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u
0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010
\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u00
10\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u
0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010
\u0010\u0010\u0010\u0010

I don’t think that I am getting the actual image data… just the address or something.
How can I get the actual image data from my const TArray data and convert it to a string so that I can send as JSON?

What are you looking for in the string format? FString has ToBlob and ToHexBlob - either of those work for you?

Hmm, I still can’t get it to work… how would I get the image data into a string? Here is what I tried:

FString frameAsFString = StringFromBinaryArray(data);
uint8 pixelData[sizeof(data.GetData())];
frameAsFString.ToHexBlob(frameAsFString, pixelData, sizeof(data.GetData()));
string frameAsString(TCHAR_TO_UTF8(pixelData)); //frameAsString is empty…

If your data contains zeros what’s to stop the characters being considered terminators? Are you looking for a raw binary blob of data (strings don’t map well to this) or can you handle something a little more readable like a string of hex values (“ab3fcd00”).

I have converted my image data to hex and it works, but really not the fastest solution.

If you have control of the receiver one solution I’ve seen floating around in these forums is to +1 on every element in the array (and -1 on receipt), you may need to deal with overflow.