iOS藍芽中的進位轉換

來源:互聯網
上載者:User

標籤:


Bluetooth4.0.jpg


最近在忙一個藍芽項目,在處理藍芽資料的時候,經常遇到進位之間的轉換,藍芽處理的是16進位(NSData),而我們習慣的計數方式是10進位,為了節省空間的,藍芽也會把16進位(NSData)拆成2進位記錄。這裡我們研究下如何在他們之間進行轉換。

假設我們要向藍芽發送0x1B9901這條資料

Byte轉NSData
Byte value[3]={0};value[0]=0x1B;value[1]=0x99;value[2]=0x01;NSData * data = [NSData dataWithBytes:&value length:sizeof(value)];//發送資料[self.peripheral writeValue:data forCharacteristic:self.write type:CBCharacteristicWriteWithoutResponse];
  • 優點:這種方法比較簡單,沒有進行轉換,直接一個位元組一個位元組的拼裝好發送出去。
  • 缺點:當發送資料比較長時會很麻煩,而且不易更改。
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;}//發送資料[self.peripheral writeValue:[self hexToBytes:@"1B9901"] forCharacteristic:self.write type:CBCharacteristicWriteWithoutResponse];
  • 優點:比較直觀,可以一次轉換一長條資料,對於一些功能簡單的藍芽程式,這種轉換能處理大部分情況。
  • 缺點:只能發送一些固定的指令,不能參與計算。
求校正和

接下來探討下發送的資料需要計算的情況。
最常用的發送資料需要計算的情境是求校正和(CHECKSUM)。這個根據硬體廠商來定,常見的求校正和的規則有:

  • 如果發送資料長度為n位元組,則CHECKSUM為前n-1位元組之和的低位元組
  • CHECKSUM=0x100-CHECKSUM(上一步的校正和)

如果我要發送帶上校正和的0x1B9901,方法就是:

- (NSData *)getCheckSum:(NSString *)byteStr{int length = (int)byteStr.length/2;NSData *data = [self hexToBytes:byteStr];Byte *bytes = (unsigned char *)[data bytes];Byte sum = 0;for (int i = 0; i<length; i++) {    sum += bytes[i];}int sumT = sum;int at = 256 -  sumT;printf("校正和:%d\n",at);if (at == 256) {    at = 0;}NSString *str = [NSString stringWithFormat:@"%@%@",byteStr,[self ToHex:at]];return [self hexToBytes:str];}//將十進位轉化為十六進位- (NSString *)ToHex:(int)tmpid{NSString *nLetterValue;NSString *str [email protected]"";int ttmpig;for (int i = 0; i<9; i++) {    ttmpig=tmpid%16;    tmpid=tmpid/16;    switch (ttmpig)    {        case 10:            nLetterValue [email protected]"A";break;        case 11:            nLetterValue [email protected]"B";break;        case 12:            nLetterValue [email protected]"C";break;        case 13:            nLetterValue [email protected]"D";break;        case 14:            nLetterValue [email protected]"E";break;        case 15:            nLetterValue [email protected]"F";break;        default:            nLetterValue = [NSString stringWithFormat:@"%u",ttmpig];    }    str = [nLetterValue stringByAppendingString:str];    if (tmpid == 0) {        break;    }}//不夠一個位元組湊0if(str.length == 1){    return [NSString stringWithFormat:@"0%@",str];}else{    return str;}}//發送資料NSData *data = [self getCheckSum:@"1B9901"];//data=<1b99014b>[self.peripheral writeValue:data forCharacteristic:self.write type:CBCharacteristicWriteWithoutResponse];
拆分資料

這種是比較麻煩的,舉個栗子:在傳輸某條資訊時,我想把時間放進去,不能用時間戳記,還要節省空間的,這樣就出現了一種新的方式儲存時間。
這裡再補充一些C語言知識:

  • 一個位元組8位(bit)
  • char 1位元組 int 4位元組 unsigned 2位元組 float 4位元組

儲存時間的條件是:

  • 只用四個位元組(32位)
  • 前5位表示年(從2000年算起),接著4位表示月,接著5位表示日,接著5位表示時,接著6位表示分,接著3位表示星期,剩餘4位保留。

這樣直觀的解決辦法就是分別取出現在時間的年月日時分星期,先轉成2進位,再轉成16進位發出去。當然你這麼寫進去,讀的時候就要把16進位資料先轉成2進位再轉成10進位顯示。我們就按這個簡單粗暴的思路來,準備工作如下:

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

有了這幾種轉換函式,完成上面的功能就容易多了,具體怎麼操作這裡就不寫一一出來了。但總感覺怪怪的,這麼一個小功能怎麼要寫這麼一大堆代碼,當然還可以用C語言的方法去解決。這裡主要是為了展示iOS中資料如何轉換,C語言的實現方法這裡就不寫了,有興趣的同學可以研究下。

附帶兩個函數

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

NSData轉int
接受到的資料0x00000a0122

//4位元組表示的intNSData *intData = [data subdataWithRange:NSMakeRange(2, 4)];int value = CFSwapInt32BigToHost(*(int*)([intData bytes]));//655650//2位元組表示的intNSData *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------------------------//補充內容,因為沒有三個位元組轉int的方法,這裡補充一個通用方法- (unsigned)parseIntFromData:(NSData *)data{   NSString *dataDescription = [data description];   NSString *dataAsString = [dataDescription substringWithRange:NSMakeRange(1, [dataDescription length]-2)];   unsigned intData = 0;   NSScanner *scanner = [NSScanner scannerWithString:dataAsString];   [scanner scanHexInt:&intData];return intData;}

這兩個轉換在某些情境下使用頻率也是挺高的,藍芽裡面的資料轉換基本也就這麼多了,希望能夠協助大家。
更多關於位元組編碼的問題,大家可以點這裡:傳送門

擴充

基於CoreBluetooth4.0架構的串連BLE4.0的Demo:你不點一下嗎



文/勇闖天涯茉莉花茶(簡書作者)
原文連結:http://www.jianshu.com/p/a5e25206df39
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。

iOS藍芽中的進位轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.