开发者

Convert char pointer (char*) to struct

开发者 https://www.devze.com 2022-12-26 04:10 出处:网络
I have a struct: //Custom packet structure. struct Us开发者_StackOverflowerPacket { __int64 pingTime;

I have a struct:

//Custom packet structure.
struct Us开发者_StackOverflowerPacket
{
 __int64 pingTime;
} CustomPacket;

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


If it's C++:

char* theCharPtr; // has your converted data

UserPacket* fromChar = reinterpret_cast<UserPacket*>(theCharPtr);


Typecast it. Here are some examples (two using type casting).

CustomPacket  customPacket;

char *          p = (char *) &customPacket;

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

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消