美文网首页
OC NSData与NSString、UIImage、Byte

OC NSData与NSString、UIImage、Byte

作者: emily_sky | 来源:发表于2016-08-25 18:59 被阅读172次

    NSData与NSString的相互转换:

    //将NSString转化为NSData
    +(NSData *)toNSData:(NSString *)str{
        NSError *error = nil;
        NSData *aData = [str dataUsingEncoding:NSUTF8StringEncoding];
        if (aData.length && error ==nil) {
            return aData;
        }else{
            return nil;
        }
    }
    //将NSData转化为NSString
    +(NSString *)transformData:(NSData *)data{
        NSError *error = nil;
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        if (string.length && error == nil) {
            return string;
        }else{
            return nil;
        }
    }
    

    NSData与UIImage的相互转换:

    //将NSData转UIImage
    UIImage *image = [UIImage imageWithData:data];
    
    //将UIImage转NSData
    NSData *data = UIImageJPEGRepresentation(image, 1.0);
    

    NSData与字典或者数组的相互转换:

    // 将字典或者数组转化为NSData
    + (NSData *)turnData:(id)theData
    {
        NSError *error = nil;
        NSData *data = [NSJSONSerialization dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted error:nil];
    
        if (data.length &&error== nil){
            return data;
        }else{
            return nil;
        }
    }
    
    // 将NSData转化为字典或者数组
    + (id)turnArrDic:(NSData *)data{
    
        NSError *error = nil;
        id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
        if (object != nil && error == nil){
            return object;
        }else{
            // 解析错误
            return nil;
        }
    
    }
    

    NSData与 Byte数组的相互转换

    //NSData-> Byte数组
    NSString *testString = @"1234567890";
    NSData *testData = [testString dataUsingEncoding:NSUTF8StringEncoding];
    Byte *testByte = (Byte *)[testData bytes];
    for(int i=0;i<[testData length];i++)
    NSLog("testByte = %d\n",testByte[i]);
    
    //Byte数组-> NSData
    short int count = 7;
    //int转2个字节
    Byte byte2[2] = {(count>>8)&0xff,count&0xff};
    NSData *adata2 = [[NSData alloc] initWithBytes:byte2 length:sizeof(byte2)];
    

    NSDictionary 与 NSString 的相互转换

    //NSDictionary 转 NSString
    - (NSString*)dictionaryToJson:(NSDictionary *)dic
    {
       NSError *parseError = nil;
       NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:dic
                                                        options:NSJSONWritingPrettyPrinted
                                                          error:&parseError];
    
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    }
    
    //NSString 转 NSDictionary 
    + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
        if (jsonString == nil) {
            return nil;
        }
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        if(error) {
            NSLog(@"json解析失败:%@",error);
            return nil;
        }
        return dic;
    }
    

    十六进制字符串转普通字符串

    -(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:4];
        return unicodeString;
    }
    

    NSData转十六进制字符串

    -(NSString *) converDataToHexString:(NSData *)data
    {
        if (data == nil) {
            return nil;
        }
        NSMutableString* hexString = [NSMutableString string];
        const unsigned char *p = [data bytes];
        for (int i=0; i < [data length]; i++) {
            [hexString appendFormat:@"%02x", *p++];
        }
        return hexString;
    }

    相关文章

      网友评论

          本文标题:OC NSData与NSString、UIImage、Byte

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