将char指针(char *)转换为struct

时间:2010-04-11 11:31:09

标签: c++

我有一个结构:

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

我已经想出如何将它转换为char *。现在我想将char *转换回struct。有什么建议吗?

2 个答案:

答案 0 :(得分:9)

如果是C ++:

char* theCharPtr; // has your converted data

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

答案 1 :(得分:5)

Typecast吧。以下是一些示例(两个使用类型转换)。

CustomPacket  customPacket;

char *          p = (char *) &customPacket;

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

希望这有帮助。