Convert char (char *) pointer to structure

I have a structure:

//Custom packet structure. struct UserPacket { __int64 pingTime; } CustomPacket; 

I already figured out how to convert it to char *. Now I want to convert char * back to a structure. Any suggestions?

+4
source share
2 answers

If it is C ++:

 char* theCharPtr; // has your converted data UserPacket* fromChar = reinterpret_cast<UserPacket*>(theCharPtr); 
+8
source

Typecast it. Here are a few examples (two using casting types).

 CustomPacket customPacket; char * p = (char *) &customPacket; CustomPacket * pPacket = (CustomPacket *) p; CustomPacket * pAlternate = &customPacket; 

Hope this helps.

+5
source

Source: https://habr.com/ru/post/1306604/


All Articles