iOS - Objective-C 字符串操作类
项目开发中自己编写的一个字符串操作相关的类:
- 16 进制数转字符串
- 计算校验码
- 转换内容为 10 进制的字符串的数据 为 长度为 2 的 16进制的字符串,其它长度字符串返回 nil
- 转换时间时分为固定长度的字符串,6时16分 -> 06:16
- 转换固定格式的时间字符串为分钟数,06:16 -> 6 * 60 + 16 = 376
StringRelatedManager.h
// // StringRelatedManager.h // FluvalSmartApp // // Created by huang zhengguo on 16/10/20. // Copyright ? 2016年 huang zhengguo. All rights reserved. // @interface StringRelatedManager : BaseBleObject /* 16进制转字符串 */ + (NSString *)hexToStringWithData:(NSData *)data; /* 计算16进制校验码 */ + (NSString *)calculateXORWithArray:(NSArray *)array; + (NSString *)calculateXORWithString:(NSString *)str; //转换内容为10进制的数据字符串为长度为2的16进制的字符串,其它长度字符串返回nil + (NSString *)convertToHexStringWithString:(NSString *)str; + (NSString *)generateTimeStrWithHour:(NSInteger)hour minute:(NSInteger)minute; + (NSInteger)generateMinutesWithHourMinuteTimeStr:(NSString *)str; + (NSString *)generateTimeStrWithTimeStr:(NSString *)timeStr; + (NSString *)convertFormatDateToHexStr:(NSString *)dateStr; // 把两个字节(16进制)的时间节点转换为坐标轴 + (NSInteger)covertTimeToXIndexWithTimeStr:(NSString *)timeStr; // 根据字符串计算百分比 + (NSInteger)calculateColorValueWithColorStr:(NSString *)colorStr; @end
StringRelatedManager.m
// // StringRelatedManager.m // FluvalSmartApp // // Created by huang zhengguo on 16/10/20. // Copyright ? 2016年 huang zhengguo. All rights reserved. // #import "StringRelatedManager.h" @implementation StringRelatedManager // 把data数据转换成十六进制字符串 + (NSString *)hexToStringWithData:(NSData *)data{ // 把data数据转换为字节数组 Byte *bytes = (Byte *)[data bytes]; NSString *hexStr = @""; for (int i=0; i<[data 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; } + (NSString *)calculateXORWithArray:(NSArray *)array{ NSInteger hexInt = 0; NSInteger xorInt = 0; for (NSString *hexStr in array) { /* 把16进制的字符串转换为整形值 */ hexInt = strtoul([hexStr UTF8String], 0, 16); xorInt = (xorInt ^ hexInt); } NSString *hexStr = [NSString stringWithFormat:@"%0lx",(long)xorInt]; return hexStr; } + (NSString *)calculateXORWithString:(NSString *)str{ if (str.length % 2 != 0){ return nil; } NSInteger hexInt = 0; NSInteger xorInt = 0; for (int i=0; i2; i++) { NSString *subStr = [str substringWithRange:NSMakeRange(i * 2, 2)]; hexInt = strtol([subStr UTF8String], 0, 16); xorInt = (xorInt ^ hexInt); } NSString *xor = [NSString stringWithFormat:@"%0lx",(long)xorInt]; if (xor.length < 2){ xor = [NSString stringWithFormat:@"0%@",xor]; } return xor; } + (NSString *)convertToHexStringWithString:(NSString *)str{ NSInteger value = [str integerValue]; NSString *hexStr = [NSString stringWithFormat:@"%0lx",(long)value]; if (hexStr.length > 2){ return nil; } if (hexStr.length == 1){ return [NSString stringWithFormat:@"0%@",hexStr]; } return hexStr; } + (NSString *)generateTimeStrWithHour:(NSInteger)hour minute:(NSInteger)minute{ NSString *hourStr = [NSString stringWithFormat:@"%02ld",hour]; NSString *minuteStr = [NSString stringWithFormat:@"%02ld",minute]; return [NSString stringWithFormat:@"%@:%@",hourStr,minuteStr]; } + (NSString *)generateTimeStrWithTimeStr:(NSString *)timeStr{ //解析小时 NSInteger hourInt = strtol([[timeStr substringToIndex:2] UTF8String], 0, 16); //解析分钟 NSInteger minuteInt = strtol([[timeStr substringFromIndex:2] UTF8String], 0, 16); NSString *hourStr = [NSString stringWithFormat:@"%02ld",hourInt]; NSString *minuteStr = [NSString stringWithFormat:@"%02ld",minuteInt]; return [NSString stringWithFormat:@"%@:%@",hourStr,minuteStr]; } #pragma mark 把日期字符串转换为16进制字符串:17:30 + (NSString *)convertFormatDateToHexStr:(NSString *)dateStr{ NSString *hourStr = [self convertToHexStringWithString:[dateStr substringToIndex:2]]; NSString *minuteStr = [self convertToHexStringWithString:[dateStr substringFromIndex:3]]; return [NSString stringWithFormat:@"%@%@",hourStr,minuteStr]; } + (NSInteger)generateMinutesWithHourMinuteTimeStr:(NSString *)str{ NSInteger hourInt = [[str substringToIndex:2] integerValue]; NSInteger minuteInt = [[str substringFromIndex:3] integerValue]; return (hourInt * 60 + minuteInt); } #pragma mark --- 把十六进制的日期字符串转换为横坐标 + (NSInteger)covertTimeToXIndexWithTimeStr:(NSString *)timeStr{ NSInteger hourInt = 0; NSInteger minuteInt = 0; // 解析小时 hourInt = strtol([[timeStr substringToIndex:2] UTF8String], 0, 16); // 解析分钟 minuteInt = strtol([[timeStr substringFromIndex:2] UTF8String], 0, 16); //生成X轴坐标 if ((minuteInt == 0) || (minuteInt > 0 && minuteInt < 15)){ return hourInt * 2; }else if (minuteInt > 45 && minuteInt < 60){ return hourInt * 2 + 2; }else{ return hourInt * 2 + 1; } } + (NSInteger)calculateColorValueWithColorStr:(NSString *)colorStr{ // 根据model的值设置存在的值 NSString *lowByte = @""; NSString *highByte = @""; lowByte = [colorStr substringToIndex:2]; highByte = [colorStr substringFromIndex:2]; return strtol([[NSString stringWithFormat:@"%@%@",highByte,lowByte] UTF8String], 0, 16); } @end