美文网首页OC那些你不知道方法的基本使用
NSData 与 NSString 进制格式转换

NSData 与 NSString 进制格式转换

作者: JopYin | 来源:发表于2019-06-11 15:55 被阅读0次

    1.NSData与NSString常规转换

    NSData *data =[str dataUsingEncoding:NSUTF8StringEncoding];
    NSString * str  =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

    2.NSData 转成十六进制格式NSString

    例如:NSData实例转换成字符串形式输出0000b5be

    - (NSString *)convertDataToHexStr:(NSData *)data{
        if (!data || [data length] == 0) {
            return @"";
        }
        NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
        
        [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
            unsigned char *dataBytes = (unsigned char*)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;
    }
    

    3.十六进制格式NSString 转NSData

    与上面2刚好相反

    -(NSData*) convertBytesStringToData {
        NSMutableData* data = [NSMutableData data];
        int idx;
        for (idx = 0; idx+2 <= self.length; idx+=2) {
            NSRange range = NSMakeRange(idx, 2);
            NSString* hexStr = [self substringWithRange:range];
            NSScanner* scanner = [NSScanner scannerWithString:hexStr];
            unsigned int intValue;
            [scanner scanHexInt:&intValue];
            [data appendBytes:&intValue length:1];
        }
        return data;
    }
    

    4.十进制格式字符转十六进制格式字符

    - (NSString *)decimalToHex {
        long long int tmpid = [self intValue];
        NSString *nLetterValue;
        NSString *str = @"";
        long long int 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;
    }
    

    5.十六进制字符转十进制字符

    - (NSString *)hexToDecimal {
        return [NSString stringWithFormat:@"%lu",strtoul([self UTF8String],0,16)];
    }
    

    相关文章

      网友评论

        本文标题:NSData 与 NSString 进制格式转换

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