美文网首页
IOS蓝牙数据转换10进制16进制 int NSData Str

IOS蓝牙数据转换10进制16进制 int NSData Str

作者: youngZhou | 来源:发表于2017-03-30 15:59 被阅读120次

//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 - 字符串与字符串之间转换

//十六进制数字字符串转换为10进制数字字符串的。

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

unsigned int value = 0;

NSScanner *scanner = [NSScanner scannerWithString:hexNumberString];

[scanner setScanLocation:0];

[scanner scanHexInt:&value];

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

}

//十进制数字字符串转换为16进制数字字符串的。

- (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进制数

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;

}

相关文章

网友评论

      本文标题:IOS蓝牙数据转换10进制16进制 int NSData Str

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