美文网首页2017
iOS蓝牙传输数据常用到的进制转换的方法

iOS蓝牙传输数据常用到的进制转换的方法

作者: CMD独白 | 来源:发表于2016-07-27 16:56 被阅读360次

    1、Byte转NSData

    Byte value[3]={0};
    value[0]=0x1B;
    value[1]=0x99;
    value[2]=0x01;
    NSData * data = [NSData dataWithBytes:&value length:sizeof(value)];
    

    优点:这种方法比较简单,没有进行转换,直接一个字节一个字节的拼装好发送出去。
    缺点:当发送数据比较长时会很麻烦,而且不易更改。

    2、NSString转NSData

    - (NSData *)hexToBytes:(NSString *)str{
    
    NSMutableData* data = [NSMutableData data];
    int idx;
    for (idx = 0; idx+2 <= str.length; idx+=2) { 
    NSRange range = NSMakeRange(idx, 2);
     NSString* hexStr = [str substringWithRange:range]; NSScanner* scanner = [NSScanner scannerWithString:hexStr]; 
    unsigned int intValue; 
    [scanner scanHexInt:&intValue];
     [data appendBytes:&intValue length:1];
    }
    return data;
    }
    

    3、将十进制转化为十六进制

    - (NSString *)ToHex:(int)tepid{
    
    NSString *nLetterValue;
    NSString *str =@"";
    int tamping;
    for (int i = 0; i<9; i++) { 
    ttmpig=tmpid%16; 
    tmpid=tmpid/16; 
    switch (ttmpig) { 
    case 10: 
    nLetterValue =@"A";
    break; 
    case 11: 
    nLetterValue =@"B";
    break; 
    case 12: 
    nLetterValue =@"C";
    break; 
    case 13: 
    nLetterValue =@"D";
    break; 
    case 14: 
    nLetterValue =@"E";
    break; 
    case 15:
     nLetterValue =@"F";
    break; 
    default: 
    nLetterValue = [NSString stringWithFormat:@"%u",ttmpig]; } 
    str = [nLetterValue stringByAppendingString:str];
     if (tmpid == 0) { 
    break;
     }
    }
    //不够一个字节凑0
    if(str.length == 1){
     return [NSString stringWithFormat:@"0%@",str];
    }else{ 
    return str;
    }
    }
    

    4、10进制转2进制

    - (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;
    }
    
    

    5、2进制转10进制

    - (NSString *)toDecimalWithBinary:(NSString *)binary{
    int ll = 0 ;
    int temp = 0 ;
    for (int i = 0; i < binary.length; i ++){ 
    temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue]; 
    temp = temp * powf(2, binary.length - i - 1); 
    ll += temp;
    }
    NSString * result = [NSString stringWithFormat:@"%d",ll];
    return result;
    }
    
    

    6、16进制和2进制互转

    - (NSString *)getBinaryByhex:(NSString *)hex binary:(NSString *)binary{
    NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] init];
    hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [hexDic setObject:@"0000" forKey:@"0"];
    [hexDic setObject:@"0001" forKey:@"1"];
    [hexDic setObject:@"0010" forKey:@"2"];
    [hexDic setObject:@"0011" forKey:@"3"];
    [hexDic setObject:@"0100" forKey:@"4"];
    [hexDic setObject:@"0101" forKey:@"5"];
    [hexDic setObject:@"0110" forKey:@"6"];
    [hexDic setObject:@"0111" forKey:@"7"];
    [hexDic setObject:@"1000" forKey:@"8"];
    [hexDic setObject:@"1001" forKey:@"9"];
    [hexDic setObject:@"1010" forKey:@"a"];
    [hexDic setObject:@"1011" forKey:@"b"];
    [hexDic setObject:@"1100" forKey:@"c"];
    [hexDic setObject:@"1101" forKey:@"d"];
    [hexDic setObject:@"1110" forKey:@"e"];
    [hexDic setObject:@"1111" forKey:@"f"];
    NSMutableString *binaryString=[[NSMutableString alloc] init];
    if (hex.length) { 
    for (int i=0; i<[hex length]; i++) {
     NSRange rage; 
    rage.length = 1;
     rage.location = i; 
    NSString *key = [hex substringWithRange:rage]; [binaryString appendString:hexDic[key]]; 
    }
    }else{
     for (int i=0; i<binary.length; i+=4) {
     NSString *subStr = [binary substringWithRange:NSMakeRange(i, 4)]; 
    int index = 0; 
    for (NSString *str in hexDic.allValues) { 
    index ++;
     if ([subStr isEqualToString:str]) { 
    [binaryString appendString:hexDic.allKeys[index-1]]; break;
     } 
    } 
    }
    }
    return binaryString;
    }
    
    

    7、int转NSData

    - (NSData *) setId:(int)Id {
    //用4个字节接收
    Byte bytes[4];
    bytes[0] = (Byte)(Id>>24);
    bytes[1] = (Byte)(Id>>16);
    bytes[2] = (Byte)(Id>>8);
    bytes[3] = (Byte)(Id);
    NSData *data = [NSData dataWithBytes:bytes length:4];
    }
    

    8、NSData转int

    接受到的数据0x00000a0122
    
    //4字节表示的int
    NSData *intData = [data subdataWithRange:NSMakeRange(2, 4)];
    int value = CFSwapInt32BigToHost(*(int*)([intData bytes]));//655650
    
    //2字节表示的int
    NSData *intData = [data subdataWithRange:NSMakeRange(4, 2)];int value = CFSwapInt16BigToHost(*(int*)([intData bytes]));//290
    
    //1字节表示的intchar *bs = (unsigned char *)[[data subdataWithRange:NSMakeRange(5, 1) ] bytes];int value = *bs;//34
    
    

    9、NSString转化为UNICODE String

    (NSString*)fname = @“Test”;
    
    char fnameStr[10];
    
    memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
    
    
    

    10、NSString转化为char

    (NSString*)fname = @“Test”;
    
    char fnameStr[10];
    
    fnameStr =[fname UTF8String];
    

    11、char -> NSData

    方法一:
    
       char * postData = "TEST";
       NSData *data = [NSData dataWithBytes:postData length:strlen(postData)];
    
     方法二:
    
        转换为NSString: - (id)initWithUTF8String:(const char *)bytes
        然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
    

    12、NSData ->char

     NSData returnData ;
    
      char* bu=[returnData bytes];
    

    13、NSData->NSString

    NSString* aStr;
    aStr = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
    

    14、NSString->NSData

    NSData* aData;
    aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];
    

    15、NSData 与 Byte

    NSString *testString = @"1234567890";
    
    NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
    
    Byte *testByte = (Byte *)[testData bytes];
    
    for(int i=0;i<[testData length];i++)
    
    printf("testByte = %d\n",testByte[i]);
    

    16、Byte数组-> NSData

    Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
    
    NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
    
    

    17、Byte数组->16进制数

    Byte *bytes = (Byte *)[aData bytes];
    
    NSString *hexStr=@"";
    
    for(int i=0;i<[encryData length];i++)
    
    {
    
    NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
    
    if([newHexStr length]==1)
    
    hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
    
    else 
    
    hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    
    }
    
    NSLog(@"bytes 的16进制数为:%@",hexStr);
    
    
    

    18、将16进制数据转化成Byte 数组

    NSString *hexString = @"3e435fab9c34891f"; //16进制字符串
    
    int j=0;
    
    Byte bytes[128];  ///3ds key的Byte 数组, 128位
    
    for(int i=0;i<[hexString length];i++)
    
    {
    
    int int_ch;  /// 两位16进制数转化后的10进制数
    
     
    
    unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
    
    int int_ch1;
    
    if(hex_char1 >= '0' && hex_char1 <='9')
    
    int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
    
    else if(hex_char1 >= 'A' && hex_char1 <='F')
    
    int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
    
    else 
    
    int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
    
    i++;
    
     
    
    unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
    
    int int_ch2;
    
    if(hex_char2 >= '0' && hex_char2 <='9')
    
    int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
    
    else if(hex_char1 >= 'A' && hex_char1 <='F')
    
    int_ch2 = hex_char2-55; //// A 的Ascll - 65
    
    else 
    
    int_ch2 = hex_char2-87; //// a 的Ascll - 97
    
     
    
    int_ch = int_ch1+int_ch2;
    
    NSLog(@"int_ch=%d",int_ch);
    
    bytes[j] = int_ch;  ///将转化后的数放入Byte数组里
    
    j++;
    
    }
    
    NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];
    
    NSLog(@"newData=%@",newData);
    
    

    19、将一个8个字节的字符串转化成NSData

    - (NSData *)change:(NSString *)string Length:(int)length{
    
        Byte byte[8];
        memset(byte, 0, 8 * sizeof(Byte));
        NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];
        Byte *stringByte = (Byte *)[data bytes];
        memcpy(byte + 0*sizeof(Byte), stringByte , length*sizeof(Byte));
        
        return [NSData dataWithBytes:byte length:8];
    }
    

    相关文章

      网友评论

      • 逸风lovely:你好!请问你有做过BLE传输,提高传输速率??
        CMD独白:尴尬了,我们之前做返回数据,如果返回的太快移动端这边就会好卡
        逸风lovely:@CMD独白 额,我们现在能达到60ms一个包
        CMD独白:蓝牙传输每次传20个字节一个包,最快的话200毫秒一个包
      • 8290a89231af:楼主 如何获取byte数组的长度 有研究过吗
        学_iOS:@cheng080010 把你的byte数组依照博客上写的转成NSData类型的 然后byte数组的长度就等于data的长度[data length]
        CMD独白:@cheng080010 获取数组长度可以直接这样sizeof(数组)
        CMD独白:@cheng080010 没有研究过,我用的时候都是直接定义数组的长度的
      • 孤独雪域:沙发了,以后也许会用到

      本文标题:iOS蓝牙传输数据常用到的进制转换的方法

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