Hardy Le Roux
Hardy Le Roux

Reputation: 1529

How to extract a GUID from a binary packet in C++?

I have a object containing multiple variables in C# that is marshaled to binary format and sent over a socket connection to a C++ application. The c++ application receives the binary data in the format of TArray < uint8 >. One of the fields that was serialized in C# is a GUID. I am trying to deserialize the the packet and populate a C++ GUID.

First I load the binary data using a memory reader and going to the byte location where the GUID field should be:

FMemoryReader FromBinary = FMemoryReader(TheBinaryArray, true);
FromBinary.Seek(9);

The GUID field should be 16 bytes in the binary. So I create a 16 byte field in c++

uint32 Data[4];

and populate the data

FromBinary << Data[0];
FromBinary << Data[1];
FromBinary << Data[2];
FromBinary << Data[3];

create a new GUID

GUID guid;

Now I dont know how to populate the GUID with the data?

Upvotes: 0

Views: 653

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597111

A GUID does not consist of 4 uint32 values. It consists of 1 uint32, 2 uint16, and 8 uint8 values.

You can copy the raw bytes from the array into the GUID variable (why are you using uint32 Data[4] instead of uint8 Data[16]?). You can use memcpy() or CopyMemory() for that, eg:

GUID guid;
memcpy(&guid, &Data[0], 16);
//CopyMemory(&guid, &Data[0], 16);

Or you can simply type-cast the memory address of the first byte in the array to a GUID* and then dereference the pointer and assign it to the GUID variable:

GUID guid;
guid = *((GUID*)&Data[0]);

Or, simply get rid of the array altogether and read the bytes from the memory reader into the GUID variable directly (if you are able to pass a void* pointer to the reader and not an array).

Upvotes: 2

Related Questions