Converting multiple integers into an integer that represents their byte arrays combined?

I’m trying to replicate what this C# code is doing:

public bool SendCommand(ushort command) {

	// Make three byte arrays representing the values of thee integers (two unsigned shorts and an unsigned int to be exact, but it doesn't matter) called command, versionNumber and payloadSize
	byte[] commandB = BitConverter.GetBytes(command);
	ushort versionNumber = 1;
	byte[] versionNumberB = BitConverter.GetBytes(versionNumber);
	uint payloadSize = 0;
	byte[] payloadSizeB = BitConverter.GetBytes(payloadSize);
	
	// Concatenate the byte arrays into one block:
	byte[] datablock = new byte[commandB.Length + versionNumberB.Length + payloadSizeB.Length];
	System.Buffer.BlockCopy(commandB, 0, datablock, 0, commandB.Length);
	System.Buffer.BlockCopy(versionNumberB, 0, datablock, commandB.Length, versionNumberB.Length);
	System.Buffer.BlockCopy(payloadSizeB, 0, datablock, commandB.Length + versionNumberB.Length, payloadSizeB.Length);
	
	// Send the datablock through TCP, don't mind what this is m_stream is doing. Just trust me, that's what it does.
	m_stream.Write(datablock, 0, datablock.Length);

}

It is using .NET features, namely BitConverter.GetBytes, to convert the integers “command”, “versionNumber” and “payloadSize” into byte arrays, then combines them into one byte array, and sends that.

In UE4, to send things over TCP the Socket->Send functionality is used. However, it takes a uint8* as a parameter for the data it is sending, and not a byte array. I could simply use uint8(myInteger) to convert that “command” integer into a thing that is sendable, but I need to combine the three integers before sending them, like what System.Buffer.BlockCopy does, so that the raw binary data sent by TCP would be the same as the one sent by that C# script.

How do I do that?

p.s. and how do I determine the size of the data to send? I’m an absolute noob in this entire networking/tcp/whatever thing.


EDIT: byte is apparently non-existent in UE4, and i need to use chars instead. how? please help I’m drowning here

uint8* is exactly array. In C++ any pointer can function as array and there really not array type, when you place [1] at the end it will read next byte (since uint8 is equivalent of byte) in next memory address. Thats why raw byta functions require you to input size of what he need to read, because you can’t read size of array in C++ (or else you program it to do so, thats why we have TArray). In C++ it will look like this:

bool SendCommand(uint16 command)  {

     uint16 versionNumber = 1;
     uint32 payloadSize = 0; //i don't know what size you use here, i assume it higher size then short so 32

     uint8* datablock = new uint8[8]; //16+16+32 in bits means, 2+2+4 in bytes

     //we copy varables to datablock

     FMemory::Memcpy(datablock, &command, 2);
     FMemory::Memcpy(datablock + 2, &versionNumber, 2);
     FMemory::Memcpy(datablock + 4, &payloadSize, 4);

    Socket->Send(datablock, 8);

    delete datablock; //for safety

}

There you got, but in C++ you can make it a lot nicer, just make struct, because C++ place them in order (if i’m not mistaken, but i seen lot of binery protocol codes do that)

struct FNetCommand {
     uint16 Command
     uint16 VersionNumber;
     uint32 PayloadSize;
}

bool SendCommand(FNetCommand command)  {

     uint8* datablock = new uint8[8]; 
     FMemory::Memcpy(datablock, &command, 8);
     Socket->Send(datablock, 8);
     delete datablock; 

}

thanks so much for this in-depth explanation and the code! this solves my problem! turns out i misunderstood the way C++ handles arrays