IOS藍(lán)牙數(shù)據(jù)轉(zhuǎn)換10進(jìn)制16進(jìn)制 int NSData String part2

//NSdata 轉(zhuǎn)16進(jìn)制字符串

- (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 轉(zhuǎn)16進(jìn)制字符串

- (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 - 字符串與字符串之間轉(zhuǎn)換

//十六進(jìn)制數(shù)字字符串轉(zhuǎn)換為10進(jìn)制數(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];

}

//十進(jìn)制數(shù)字字符串轉(zhuǎn)換為16進(jìn)制數(shù)字字符串的。

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

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

}

// 十六進(jìn)制轉(zhuǎn)換為普通字符串的。

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

}

//普通字符串轉(zhuǎn)換為十六進(jìn)制的。

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

{

NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];

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

//下面是Byte 轉(zhuǎn)換為16進(jìn)制。

NSString *hexStr=@"";

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

{

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

if([newHexStr length]==1)

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

else

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

}

return hexStr;

}

#pragma mark - Dictionary 轉(zhuǎn) 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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容