ios十进制、十六进制字符串,byte,data等之间的转换
尊重原创精神,此文章为转载,里面的转换帮了我很的大忙,我决定都记录下来方便以后查看,同时我会定期更新工作中遇到的各种转换算法留作记录。
十进制->十六进制
Byte bytes[] = {0xA6,0x27,0x0A};
NSString *strIdL = [NSString stringWithFormat:@"%@",[[NSString alloc]initWithFormat:@"%02lx",(long)bytes[0]]];
十六进制->十进制
NSString *rechargeInfo = @"0xff055008";
NSString *cardId2 = [rechargeInfo substringWithRange:NSMakeRange(2,2)];
cardId2 = [NSString stringWithFormat:@"%ld",strtoul([cardId2 UTF8String],0,16)];
NSString *str = @"0xff055008";
//先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型
unsigned long red = strtoul([str UTF8String],0,16);
//strtoul如果传入的字符开头是“0x”,那么第三个参数是0,也是会转为十六进制的,这样写也可以:
unsigned long red = strtoul([@"0x6587"UTF8String],0,0);
NSLog(@"转换完的数字为:%lx",red);
字符串处理方法
+ (NSString *)cutString:(NSString *)str{
NSString *targetStr=nil;
if (str.length>9) {
targetStr=[str substringFromIndex:9];
}
return targetStr;
}
字符串补零操作
+ (NSString *)addZero:(NSString *)str withLength:(int)length{
NSString *string = nil;
if (str.length==length) {
return str;
}
if (str.length<length) {
NSUInteger inter = length-str.length;
for (int i=0;i< inter; i++) {
string = [NSString stringWithFormat:@"0%@",str];
str = string;
}
}
return string;
}
+ (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;
NSLog(@"myBuffer is %c",myBuffer[i /2] );
}
NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
NSLog(@"———字符串=======%@",unicodeString);
return unicodeString;
}
10进制->16进制
+ (NSString *)ToHex:(longlong int)tmpid{
NSString *nLetterValue;
NSString *str =@"";
long longint ttmpig;
for (int i =0; i<9; i++) {
ttmpig=tmpid%16;
tmpid=tmpid/16;
switch (ttmpig)
{
case 10:
nLetterValue =@"A";break;
case 11:
nLetterValue =@"B";break;
case 12:
nLetterValue =@"C";break;
case 13:
nLetterValue =@"D";break;
case 14:
nLetterValue =@"E";break;
case 15:
nLetterValue =@"F";break;
default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
return str;
}
16进制字符串->NSData
+ (NSMutableData *)convertHexStrToData:(NSString *)str {
if (!str || [str length] == 0) {
return nil;
}
NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
NSRange range;
if ([str length] %2 == 0) {
range = NSMakeRange(0,2);
} else {
range = NSMakeRange(0,1);
}
for (NSInteger i = range.location; i < [strlength]; i += 2) {
unsigned int anInt;
NSString *hexCharStr = [str substringWithRange:range];
NSScanner *scanner = [[NSScanner alloc]initWithString:hexCharStr];
[scanner scanHexInt:&anInt];
NSData *entity = [[NSData alloc]initWithBytes:&anIntlength:1];
[hexData appendData:entity];
range.location += range.length;
range.length = 2;
}
return hexData;
}
+ (NSString *)convertDataToHexStr:(NSData *)data {
if (!data || [data length] == 0) {
return @"";
}
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(constvoid *bytes, NSRange byteRange,BOOL *stop) {
unsigned char *dataBytes = (unsignedchar*)bytes;
for (NSInteger i =0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}];
return string;
}
十进制->十六进制字符串
+ (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;
}
+ (NSString *) parseByteArray2HexString:(Byte[]) bytes{
NSMutableString *hexStr = [[NSMutableString alloc]init];
int i = 0;
if(bytes){
while (bytes[i] != '\0')
{
NSString *hexByte = [NSString stringWithFormat:@"%x",bytes[i] & 0xff];//16进制数
if([hexByte length]==1)
[hexStr appendFormat:@"0%@", hexByte];
else
[hexStr appendFormat:@"%@", hexByte];
i++;
}
}
NSLog(@"bytes 的16进制数为:%@",hexStr);
return hexStr;
}
网友评论