记录蓝牙中用到的进制换算

作者: 蚯小麦 | 来源:发表于2017-08-16 16:58 被阅读10次

最近蓝牙项目中,蓝牙手环中的指令比较多,比如同步时间,发送震动指令,来点提醒等等,根据和硬件定义的规则来发送指令。
比如同步时间的指令:

<ab000009 00000000 01000100 04 +年月日时分秒>
这里的年月日时分秒都有一定的位数限制
比如年是6位,月是4位,日是5位,这就需要将本地时间给拆分,封装出一个方法算出这串指令

直接上代码

/**
 同步时间的指令
 根据当前年月日时分秒 根据规则先转二进制,然后四位一组转十六进制
 @return 返回的数据
 */
+ (NSString *)senderTimeCommand {
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *nowdate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    comps = [calendar components:unitFlags fromDate:nowdate];
    
    NSString *year = [[NSString stringWithFormat:@"%ld",(long)[comps year]] substringFromIndex:2];
    NSString *month = [NSString stringWithFormat:@"%ld",(long)[comps month]];
    NSString *day = [NSString stringWithFormat:@"%ld",(long)[comps day]];
    NSString *hour = [NSString stringWithFormat:@"%ld",(long)[comps hour]];
    NSString *minute = [NSString stringWithFormat:@"%ld",(long)[comps minute]];
    NSString *second = [NSString stringWithFormat:@"%ld",(long)[comps second]];
    // 十进制转换为二进制带位数
    NSString *yearByte = [self toBinarySystemWithDecimalSystem:[year intValue] length:6];//6位
    NSString *monthByte = [self toBinarySystemWithDecimalSystem:[month intValue] length:4];//4位
    NSString *dayByte = [self toBinarySystemWithDecimalSystem:[day intValue] length:5];//5位
    NSString *hourByte = [self toBinarySystemWithDecimalSystem:[hour intValue] length:5];//5位
    NSString *minuteByte = [self toBinarySystemWithDecimalSystem:[minute intValue] length:6];//6位
    NSString *secondByte = [self toBinarySystemWithDecimalSystem:[second intValue] length:6];//6位
    
    //将时间的二进制组合起来
    NSString *byteStr = [NSString stringWithFormat:@"%@%@%@%@%@%@",yearByte,monthByte,dayByte,hourByte,minuteByte,secondByte];
   //每4位转换成十六进制
    NSString *byte0 = [byteStr substringToIndex:4];
    NSString *byte1 = [byteStr substringWithRange:NSMakeRange(4, 4)];
    NSString *byte2 = [byteStr substringWithRange:NSMakeRange(8, 4)];
    NSString *byte3 = [byteStr substringWithRange:NSMakeRange(12, 4)];
    NSString *byte4 = [byteStr substringWithRange:NSMakeRange(16, 4)];
    NSString *byte5 = [byteStr substringWithRange:NSMakeRange(20, 4)];
    NSString *byte6 = [byteStr substringWithRange:NSMakeRange(24, 4)];
    NSString *byte7 = [byteStr substringWithRange:NSMakeRange(28, 4)];
    

    NSString *finalStr = [NSString stringWithFormat:@"ab000009000000000100010004%@%@%@%@%@%@%@%@",[self getHexByBinary:byte0],[self getHexByBinary:byte1],[self getHexByBinary:byte2],[self getHexByBinary:byte3],[self getHexByBinary:byte4],[self getHexByBinary:byte5],[self getHexByBinary:byte6],[self getHexByBinary:byte7]];
    BYLog(@"%@",finalStr);
    return finalStr;
}

上面用到的方法

//  十进制转二进制
- (NSString *)toBinarySystemWithDecimalSystem:(int)num length:(int)length
{
    int remainder = 0;      //余数
    int divisor = 0;        //除数
    
    NSString * prepare = @"";
    
    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%d",remainder];
        
        if (divisor == 0)
        {
            break;
        }
    }
    //倒序输出
    NSString * result = @"";
    for (int i = length -1; i >= 0; i --)
    {
        if (i <= prepare.length - 1) {
            result = [result stringByAppendingFormat:@"%@",
                      [prepare substringWithRange:NSMakeRange(i , 1)]];
            
        }else{
            result = [result stringByAppendingString:@"0"];
            
        }
    }
    return result;
}

二进制转16进制


//2zhuan16
- (NSString *)getHexByBinary:(NSString *)binary {
    
    NSMutableDictionary *binaryDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [binaryDic setObject:@"0" forKey:@"0000"];
    [binaryDic setObject:@"1" forKey:@"0001"];
    [binaryDic setObject:@"2" forKey:@"0010"];
    [binaryDic setObject:@"3" forKey:@"0011"];
    [binaryDic setObject:@"4" forKey:@"0100"];
    [binaryDic setObject:@"5" forKey:@"0101"];
    [binaryDic setObject:@"6" forKey:@"0110"];
    [binaryDic setObject:@"7" forKey:@"0111"];
    [binaryDic setObject:@"8" forKey:@"1000"];
    [binaryDic setObject:@"9" forKey:@"1001"];
    [binaryDic setObject:@"A" forKey:@"1010"];
    [binaryDic setObject:@"B" forKey:@"1011"];
    [binaryDic setObject:@"C" forKey:@"1100"];
    [binaryDic setObject:@"D" forKey:@"1101"];
    [binaryDic setObject:@"E" forKey:@"1110"];
    [binaryDic setObject:@"F" forKey:@"1111"];
    
    if (binary.length % 4 != 0) {
        
        NSMutableString *mStr = [[NSMutableString alloc]init];;
        for (int i = 0; i < 4 - binary.length % 4; i++) {
            
            [mStr appendString:@"0"];
        }
        binary = [mStr stringByAppendingString:binary];
    }
    NSString *hex = @"";
    for (int i=0; i<binary.length; i+=4) {
        
        NSString *key = [binary substringWithRange:NSMakeRange(i, 4)];
        NSString *value = [binaryDic objectForKey:key];
        if (value) {
            
            hex = [hex stringByAppendingString:value];
        }
    }
    return hex;
}


然后发送这串指令,手环时间就同步了。

打工是不可能的.gif

相关文章

  • 记录蓝牙中用到的进制换算

    最近蓝牙项目中,蓝牙手环中的指令比较多,比如同步时间,发送震动指令,来点提醒等等,根据和硬件定义的规则来发送指令。...

  • 类型转换

    做蓝牙功能的时候用到了很多类型转换,记录一下。 1、字符串转data 2、data转字符串 3、十六进制字符串转中...

  • 我的第一个硬件联调app 资料

    iOS蓝牙中的进制转换,数据格式转换 最近在忙一个蓝牙项目,在处理蓝牙数据的时候,经常遇到进制之间的转换,蓝牙处理...

  • 手工进行 进制转换 的一种方式

    基本原理:用 二进制 做中转 二进制进制位换算方式: 二进制进制位换算方式说明:比如十进制 6666 = 6 * ...

  • 171. Excel Sheet Column Number

    171. Excel Sheet Column Number【思路】 进制换算;

  • 无标题文章

    在处理蓝牙数据的时候,经常遇到进制之间的转换,蓝牙处理的是16进制(NSData),而我们习惯的计数方式是10进制...

  • 无标题文章

    在处理蓝牙数据的时候,经常遇到进制之间的转换,蓝牙处理的是16进制(NSData),而我们习惯的计数方式是10进制...

  • iOS-蓝牙的简单使用

    蓝牙实现方案 之前项目有用到蓝牙,这里记录一下蓝牙的一些简单使用. iOS提供了4个用于实现蓝牙连接的方案: 1....

  • iOS 关于异或运算

    本人开发的都是和蓝牙有关在程序中 经常要用到异或运算 现在给大家罗列出来 1校验位异或 传入16进制串 -(NSD...

  • iOS蓝牙中的进制转换

    最近在忙一个蓝牙项目,在处理蓝牙数据的时候,经常遇到进制之间的转换,蓝牙处理的是16进制(NSData),而我们习...

网友评论

    本文标题:记录蓝牙中用到的进制换算

    本文链接:https://www.haomeiwen.com/subject/luoirxtx.html