IOS藍牙數(shù)據(jù)轉換10進制16進制 int NSData String part2

//NSdata 轉16進制字符串

- (NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces withData:(NSData *)data

{

const unsigned char* bytes = (const unsigned char*)[data bytes];

NSUInteger nbBytes = [data length];

//If spaces is true, insert a space every this many input bytes (twice this many output characters).

static const NSUInteger spaceEveryThisManyBytes = 4UL;

//If spaces is true, insert a line-break instead of a space every this many spaces.

static const NSUInteger lineBreakEveryThisManySpaces = 4UL;

const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;

NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0);

NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];

for(NSUInteger i=0; i<nbBytes;){

[hex appendFormat:@"%02X", bytes[i]];

//We need to increment here so that the every-n-bytes computations are right.

++i;

if (spaces) {

if (i % lineBreakEveryThisManyBytes == 0) [hex appendString:@":"];

else if (i % spaceEveryThisManyBytes == 0) [hex appendString:@":"];

}

}

return hex;

}


//NSdata 轉16進制字符串

- (NSString*)hexRepresentationWithSymbol:(NSString *)symbol withData:(NSData *)data

{

const unsigned char* bytes = (const unsigned char*)[data bytes];

NSUInteger nbBytes = [data length];

//If spaces is true, insert a space every this many input bytes (twice this many output characters).

//static const NSUInteger spaceEveryThisManyBytes = 4UL;

//If spaces is true, insert a line-break instead of a space every this many spaces.

//static const NSUInteger lineBreakEveryThisManySpaces = 4UL;

//const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;

NSUInteger strLen = 2*nbBytes;

NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];

for(NSUInteger i=0; i<nbBytes;) {

[hex appendFormat:@"%02X%@", bytes[i], symbol];

//We need to increment here so that the every-n-bytes computations are right.

++i;

}

[hex deleteCharactersInRange:NSMakeRange(hex.length - 1, 1)];

return hex;

}


#pragma mark - 字符串與字符串之間轉換

//十六進制數(shù)字字符串轉換為10進制數(shù)字字符串的坯钦。

- (NSString *)hexNumberStringToNumberString:(NSString *)hexNumberString{

unsigned int value = 0;

NSScanner *scanner = [NSScanner scannerWithString:hexNumberString];

[scanner setScanLocation:0];

[scanner scanHexInt:&value];

return [NSString stringWithFormat:@"%d",value];

}

//十進制數(shù)字字符串轉換為16進制數(shù)字字符串的。

- (NSString *)numberStringToHexNumberString:(NSString *)numberString{

return [NSString stringWithFormat:@"%x",[numberString intValue]];

}

// 十六進制轉換為普通字符串的印叁。

- (NSString *)stringFromHexString:(NSString *)hexString

{

char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);

bzero(myBuffer, [hexString length] / 2 + 1);

for (int i = 0; i < [hexString length] - 1; i += 2) {

unsigned int anInt;

NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];

NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];

[scanner scanHexInt:&anInt];

myBuffer[i / 2] = (char)anInt;

}

NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:NSUTF8StringEncoding];

return unicodeString;

}

//普通字符串轉換為十六進制的觉鼻。

- (NSString *)hexStringFromString:(NSString *)string

{

NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];

Byte *bytes = (Byte *)[myD bytes];

//下面是Byte 轉換為16進制。

NSString *hexStr=@"";

for(int i=0;i<[myD length];i++)

{

NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16進制數(shù)

if([newHexStr length]==1)

hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];

else

hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];

}

return hexStr;

}

#pragma mark - Dictionary 轉 Json String

-(NSString *)dictionaryToJsonStr:(id)dic

{

if (dic==nil) {

return nil;

}

NSError *error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic

options:NSJSONWritingPrettyPrinted

error:&error];

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

jsonString = [jsonString stringByReplacingOccurrencesOfString:@" " withString:@""];

//NSLog(@"JSON String = %@", jsonString);

return jsonString;

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末掷倔,一起剝皮案震驚了整個濱河市耍缴,隨后出現(xiàn)的幾起案子辩蛋,更是在濱河造成了極大的恐慌苹熏,老刑警劉巖碟贾,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異轨域,居然都是意外死亡袱耽,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門干发,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扛邑,“玉大人,你說我怎么就攤上這事铐然。” “怎么了恶座?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵搀暑,是天一觀的道長。 經(jīng)常有香客問我跨琳,道長自点,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任脉让,我火速辦了婚禮桂敛,結果婚禮上功炮,老公的妹妹穿的比我還像新娘。我一直安慰自己术唬,他們只是感情好薪伏,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著粗仓,像睡著了一般嫁怀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上借浊,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天塘淑,我揣著相機與錄音,去河邊找鬼蚂斤。 笑死存捺,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的曙蒸。 我是一名探鬼主播捌治,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼逸爵!你這毒婦竟也來了具滴?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤师倔,失蹤者是張志新(化名)和其女友劉穎构韵,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體趋艘,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡疲恢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了瓷胧。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片显拳。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖搓萧,靈堂內(nèi)的尸體忽然破棺而出杂数,到底是詐尸還是另有隱情,我是刑警寧澤瘸洛,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布揍移,位于F島的核電站,受9級特大地震影響反肋,放射性物質(zhì)發(fā)生泄漏那伐。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望罕邀。 院中可真熱鬧畅形,春花似錦、人聲如沸诉探。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阵具。三九已至碍遍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間阳液,已是汗流浹背怕敬。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留帘皿,地道東北人东跪。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像鹰溜,于是被迫代替她去往敵國和親虽填。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容