iOS 知识点

iOS自定义协议的数据写入与解析

2018-11-07  本文已影响6人  DoflaKaiGo

首先简单说下基础概念:1 byte = 8 bit,一字节需要用8个二进制数来表示,0x00 = 0000 0000,一个十六进制数需要四位二进制表示 << n ,左移n位,相当于 * 2^n,&0xFF:相当于&1111 1111,


>> :表示向右边位移, 左边补零
<<:左位移,右边补零
i = 10011100;
i >> 3 —表示-> 10011100 -> 00010011 向右位移三位,左边补零
i << 3 —表示->10011100 -> 11100000 向左位移三位,右边补零

0xff:这里一个f 表示四位二进制,即表示 1111 1111
不失一般性:0xff00: —>表示 1111 1111 0000 0000
当一个 byte &0xff 的时候,表示和 1111 1111 做位与 运算 ,作用是保持二进制原码一致,无符号,截取 低八位,因为 和 1111 1111 做位与 运算的时候,在这个数之前的都会变成 0,所以 起到截取 低位的作用

int 转16 进制



-(NSData *)WritMessage
{
                Byte chCMD[15];
                int cul = 0;
                int iTemp;
                //boolean bPass = false;
    
                chCMD[cul++] = (Byte)0x18;
                chCMD[cul++] = (Byte)0x81;
    
                chCMD[cul++] = (Byte)0x35;
    
                chCMD[cul++] = 852%256;
                chCMD[cul++] = 852/256;
    
                //Client ID
                iTemp = (int)CNNUserInfo.Client_ID ;
                chCMD[cul++] = (Byte)(iTemp  & 0xFF);
                chCMD[cul++] = (Byte)((iTemp & 0xFF00) >> 8);
                chCMD[cul++] = (Byte)((iTemp & 0xFF0000) >> 16);
                chCMD[cul++] = (Byte)((iTemp & 0xFF000000) >> 24);
    
                //Group ID
                iTemp = (int)CNNUserInfo.Current_GroupID;
                chCMD[cul++] = (Byte)(iTemp  & 0xFF);
                chCMD[cul++] = (Byte)((iTemp & 0xFF00) >> 8);
                chCMD[cul++] = (Byte)((iTemp & 0xFF0000) >> 16);
                chCMD[cul++] = (Byte)((iTemp & 0xFF000000) >> 24);
    
                chCMD[cul++] = (Byte)0x7F;
                chCMD[cul++] = (Byte)0xF7;
    
    return [NSData dataWithBytes:chCMD length:15];
    
}

- (id)readData:(NSData *)data
{
    NSMutableDictionary *RealTimeDic = [NSMutableDictionary dictionary];
    Byte *chRevCP = (Byte*)[data bytes];
    
    NSLog(@"data is %@",RealTimeDic);
    int cul = 0;
//    cul += 2;  //0x18 0x81
    cul += 1;  //0x22
    
    int local_ack = chRevCP[cul++];
    if (local_ack == 0) {
        [RealTimeDic setObject:@(0) forKey:kDicKey_SuccessLabel];
    }else{
        [RealTimeDic setObject:@1 forKey:kDicKey_SuccessLabel];
    }
    //总数
    CNNUserInfo.Logger_Total = chRevCP[cul++];
    
    for(int i=0; i<LLBUserInfo.Logger_Total; i++)
    {
        Byte  nameByte[64];
        EquipmentModel *LoggerModel = [EquipmentModel new];
        NSMutableArray *passWayArr = [NSMutableArray new];
        Byte  snByte[10];
        for(int j=0; j<10; j++)
        {
           snByte[j] = chRevCP[cul++];
        }
        //编号
        LoggerModel.EquipmentCode= [[NSString alloc]initWithBytes:snByte length:10 encoding:LLBEncoding];
        for(int j=0; j<48; j++)
        {
            nameByte[j] = chRevCP[cul++];
        }
        //名称
        LoggerModel.EquipmentName = [[NSString alloc]initWithBytes:nameByte length:48 encoding:CNNEncoding];
        //数目
         LoggerModel.PassWayNumber = chRevCP[cul++];
        PassWayModel *onePassWay = [PassWayModel new];
        onePassWay.Type = chRevCP[cul++];
        float high = ((chRevCP[cul]&0xFF) + (chRevCP[cul+1]&0xFF)*256 + (chRevCP[cul+2]&0xFF)*256*256 + (chRevCP[cul+3]&0xFF)*256*256*256)/(float)10.0;
        onePassWay.High = high;
        cul += 4;
        onePassWay.Low = ((chRevCP[cul]&0xFF) + (chRevCP[cul+1]&0xFF)*256 + (chRevCP[cul+2]&0xFF)*256*256 + (chRevCP[cul+3]&0xFF)*256*256*256)/(float)10.0;
        cul += 4;
         onePassWay.data_dot = chRevCP[cul++];
        Byte chtemp0[8];
        for(int j = 0; j < 8; j++)
        {
            chtemp0[j] = chRevCP[cul++];
        }
        if (onePassWay.Type==8) {
             onePassWay.Symbol = [[NSString alloc]initWithBytes:chtemp0 length:8 encoding:LLBEncoding];
        }
}
上一篇下一篇

猜你喜欢

热点阅读