不同的CPU有不同的字節(jié)序類型,這些字節(jié)序是指整數(shù)在內(nèi)存中保存的順序,這個叫做主機(jī)序最常見的有兩種
1. 主機(jī)字節(jié)序是 Little endian(小端模式):將低序字節(jié)存儲在起始地址
2. 網(wǎng)絡(luò)字節(jié)序是 Big endian(大端模式):將高序字節(jié)存儲在起始地址
在網(wǎng)絡(luò)上傳輸數(shù)據(jù)時,由于數(shù)據(jù)傳輸?shù)膬啥藢?yīng)不同的硬件平臺,采用的存儲字節(jié)順序可能不一致。所以在TCP/IP協(xié)議規(guī)定了在網(wǎng)絡(luò)上必須采用網(wǎng)絡(luò)字節(jié)順序,也就是大端模式。
以 short address = 54 為例,分別看看兩種字節(jié)序存儲情況
short address = 5454;
int8_t ch[2];
#將address以大端模式進(jìn)行存儲,應(yīng)該先寫入高字節(jié),將高序字節(jié)存儲在起始地址
ch[0] = (address & 0x0ff00)>>8;
ch[1] = (address & 0x0ff);
NSLog(@"%@", [NSData dataWithBytes:ch length:2]);
得到結(jié)果:154e
若使用直接使用iOS中的方法進(jìn)行存儲得出的格式則是小端模式
short address = 5454;
//將一個long 寫入data中
NSLog(@"%@", [NSData dataWithBytes:&address length:sizeof(short)]);
得到結(jié)果:4e15
系統(tǒng)提供了大小端轉(zhuǎn)換的方法(在xcode中按住command 將鼠標(biāo)移到方法上點擊進(jìn)去可以看到更多的方法)
/* Functions for storing host endianess to big endian. */
#define OSWriteBigInt16(base, byteOffset, data)
#define OSWriteBigInt32(base, byteOffset, data)
#define OSWriteBigInt64(base, byteOffset, data)
/* Functions for storing host endianess to little endian. */
#define OSWriteLittleInt16(base, byteOffset, data)
#define OSWriteLittleInt32(base, byteOffset, data)
#define OSWriteLittleInt64(base, byteOffset, data)
在開發(fā)中可能會是這樣子的情況
- 通信協(xié)議分為兩層:mac層、應(yīng)用層
- mac層全部為小端對齊
- 應(yīng)用層數(shù)據(jù)大端對齊
mac層
| 起始符 | 目標(biāo)地址 | 原地址 | 應(yīng)用層數(shù)據(jù)長度 | 應(yīng)用層數(shù)據(jù) | 檢驗符 |
|---|---|---|---|---|---|
| 0x02 | 2Byte | 2Byte | 2Byte | 應(yīng)用層數(shù)據(jù) | CRC校驗 |
假如現(xiàn)在要用區(qū)域ID查詢該地區(qū)服務(wù)點個數(shù)
要發(fā)送的查詞命令格式是這樣(應(yīng)用層數(shù)據(jù))
| 手機(jī)查詢命令符 | 查詢命令符 | 區(qū)域編號 |
|---|---|---|
| 0x18 | 0x01 | int(4Byte大?。?/td> |
應(yīng)用層數(shù)據(jù)應(yīng)該這樣拼接
int areaId = 45;
NSMutableData *data = [[NSMutableData alloc] init];
Byte bytes[] = {0x18, 0x01};
[data appendBytes:bytes length:sizeof(bytes)];
# 將areaId以大端模式寫入數(shù)據(jù)流中
int8_t ch[4];
for(int32_t i = 0;i<4;i++){
ch[i] = ((areaId >> ((3 - i)*8)) & 0x0ff);
}
[data appendBytes:ch length:4];
或者這樣寫
int areaId = 45;
NSMutableData *data = [[NSMutableData alloc] init];
Byte bytes[] = {0x18, 0x01};
[data appendBytes:bytes length:sizeof(bytes)];
# 將areaId以大端模式寫入數(shù)據(jù)流中
Byte ch[4];
OSWriteBigInt32(ch, 0, areaId);
[data appendBytes:ch length:4];
下面開始拼接完整的協(xié)議
假設(shè)目標(biāo)地址和原地址都為0 ,應(yīng)用數(shù)據(jù)層為上面的data
short dest = 0;
short src = 0;
#完整的數(shù)據(jù)長度
Byte bytes[1+2+2+2+data.length+1];//1Byte + 2Byte + 2Byte + 2Byte + 應(yīng)用層數(shù)據(jù) + CRC校驗(1Byte)
bytes[0] = 0x02;
[self writeShort:dest bytes:bytes location:1];
[self writeShort:src bytes:bytes location:3];
[self writeShort:data.length bytes:bytes location:5];
Byte *by = (Byte *)[data bytes];
for (int i = 0; i < data.length; i ++) {
bytes[7 + i] = by[i];
}
bytes[7+data.length] = Get_Crc8(bytes, sizeof(bytes) - 1);
NSData *endData = [NSData dataWithBytes:bytes length:sizeof(bytes)];
# endData 就是要發(fā)給服務(wù)器的完整數(shù)據(jù)流,iOS可以使用 GCDAsyncSocket
// 為了方便拼接mac層數(shù)據(jù)
- (void)writeShort:(int16_t)v bytes:(Byte *)bytes location:(int)location {
# mac層數(shù)據(jù)小端對齊
OSWriteLittleInt16(bytes,location,v);
}
開發(fā)過程中可能還會遇到 double 轉(zhuǎn)成 Byte數(shù)組的情況,比如要發(fā)送經(jīng)緯度
java中有這樣的方法,先將double轉(zhuǎn)成long
double value = 4.556;
long accum = Double.doubleToRawLongBits(value);
iOS中有 NSSwapHostDoubleToBig();
double value = 4.556;
NSSwappedDouble wValue = NSSwapHostDoubleToBig(value);
long lvalue = wValue.v;
/*
typedef struct {unsigned long long v;} NSSwappedDouble;
*/
這只是關(guān)于數(shù)據(jù)的拼接,后續(xù)會加上 GCDAsyncSocket 發(fā)送消息,接收按格式解析消息