socket二进制报文

里面有帧头 字符串UTF-8 中间用0隔开 又一个字符串
byte[10] byte[0]、byte[1]是帧头 0xaa,0x55
byte[2]-byte[5]是字符串utf-8 byte[6]=0,byte[7]-byte[9]是另外的字符串
这的一个包,该怎发?

NSMutableData *data = [[NSMutableData alloc] init];

// 包头
unsigned char header[] = { 0xAA, 0x55 };
[data appendBytes:header length:sizeof(header)];

// UTF8字符串
NSString *string = @"我是UTF8";

[data appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];

// 分隔符0x00
unsigned char separator = 0;
[data appendBytes:&separator length:1];

NSString *otherString = @"其他UTF8";

[data appendData:[otherString dataUsingEncoding:NSUTF8StringEncoding]];


typedef struct{
u_int32_t first;
u_int32_t second;
}pkt;

pkt t_pkt = {0};
pkt.first = 0xaa;

NSData *data = [NSData dataWithBytes:&t_pkt length:sizeof(t_pkt)];

你可能感兴趣的:(iPhone,iOS,Objective-c)