很多时候需要将c,c++形式的struct转换为 NSData来处理。但是怎么转换呢?
假设有这么一个结构体:
struct MYINFO
{
int a;
long b;
char c;
};
struct MYINFO infoStruct;
infoStruct.a = 100;
infoStruct.b =10000;
infoStruct.c = 'c';
1. 将 infoStruct转换为NSData
NSData * msgData = [[NSData alloc]initWithBytes:&infoStruct length:sizeof(infoStruct)];
2. 将 msgData转换为 MYINFO 对象。
struct MYINFO infoStruct2;
[msgData getBytes:&infoStruct2 length:sizeof(infoStruct2)];