ios 开发之 各种数据类型之间转换 (持续补充)
1.图片转base64串及反转回图片
UIImage图片转成base64字符串:
1 UIImage *_originImage = [UIImage imageNamed:@"full_playlist_hl.png"]; 2 NSData *_data = UIImageJPEGRepresentation(_image, 1.0f);
3 NSString *_encodedImageStr = [_data base64Encoding]; 4 NSLog(@"===Encoded image:\n%@", _encodedImageStr);
Base64字符串转UIImage图片:
1 NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:_encodedImageStr]; 2 UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData]; 3 NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));
2.字符串转nsdate时间
方法一:
1 // format: yyyymmddhhmm (24-hour clock) 4 NSString *curStr = [dateString substringWithRange:NSMakeRange(0, 4)];
7 NSInteger yeardigit = [curStr integerValue]; 9 10 curStr = [dateString substringWithRange:NSMakeRange(4, 2)]; 12 13 NSInteger monthdigit = [curStr integerValue]; 14 16 curStr = [dateString substringWithRange:NSMakeRange(6, 2)]; 17 19 NSInteger daydigit = [curStr integerValue]; 20 22 curStr = [dateString substringWithRange:NSMakeRange(8, 2)]; 23 25 NSInteger hourdigit = [curStr integerValue]; 27 28 curStr = [dateString substringWithRange:NSMakeRange(10, 2)]; 29 31 NSInteger minutedigit = [curStr integerValue]; 37 NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 38 40 [dateComps setYear:yeardigit]; 41 43 [dateComps setMonth:monthdigit]; 44 46 [dateComps setDay:daydigit]; 47 49 [dateComps setHour:hourdigit]; 50 52 [dateComps setMinute:minutedigit]; 54 55 NSCalendar *calendar = [NSCalendar currentCalendar]; 56 58 NSDate *itemDate = [calendar dateFromComponents:dateComps];
方法二:
根据字符串时间格式 将字符串dateStr转化为nsdate NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"y-M-d HH:mm:ss"]; NSDate *date=[dateFormatter dateFromString:dateStr]; //这样转化为NSdate 对象 //如果想转换为:12/2/16 16:49:13代码如下 //[dateFormatter setDateFormat:@'yy/M/d H:m:s']; //NSLog(@'%@',[dateFormatter stringFromDate:date]); // NSLog(@"%@",date);
提取date中时间的年、月、星期、日、时、分和秒
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; now=[NSDate date]; comps = [calendar components:unitFlags fromDate:now]; int year=[comps year]; int week = [comps weekday]; int month = [comps month]; int day = [comps day]; int hour = [comps hour]; int min = [comps minute]; int sec = [comps second];
3.NSString(汉字)和UTF8
1 NSString* strA = [@"%E4%B8%AD%E5%9B%BD"stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 2 NSString *strB = [@"中国"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
4.JSONString转换字典
1 /*! 2 * @brief 把格式化的JSON格式的字符串转换成字典 3 * @param jsonString JSON格式的字符串 4 * @return 返回字典 5 */ 6 + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { 7 if (jsonString == nil) { 8 return nil; 9 } 10 11 NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 12 NSError *err; 13 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData 14 options:NSJSONReadingMutableContainers 15 error:&err]; 16 if(err) { 17 NSLog(@"json解析失败:%@",err); 18 return nil; 19 } 20 return dic; 21 }
5.NSData 与 NSString ,Byte数组 ,UIImage的相互转换
//1. NSData 与 NSString //NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding]; //NSString->NSData NSString *aString = @"1234abcd"; NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding]; //2.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++) printf("testByte = %d\n",testByte[i]); //Byte数组-> NSData Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23}; NSData *adata = [[NSData alloc] initWithBytes:byte length:24]; //Byte数组->16进制数 Byte *bytes = (Byte *)[aData bytes]; NSString *hexStr=@""; for(int i=0;i<[encryData 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]; } NSLog(@"bytes 的16进制数为:%@",hexStr); //16进制数->Byte数组 ///// 将16进制数据转化成Byte 数组 NSString *hexString = @"3e435fab9c34891f"; //16进制字符串 int j=0; Byte bytes[128]; ///3ds key的Byte 数组, 128位 for(int i=0;i<[hexString length];i++) { int int_ch; /// 两位16进制数转化后的10进制数 unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16) int int_ch1; if(hex_char1 >= '0' && hex_char1 <='9') int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48 else if(hex_char1 >= 'A' && hex_char1 <='F') int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65 else int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97 i++; unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位) int int_ch2; if(hex_char2 >= '0' && hex_char2 <='9') int_ch2 = (hex_char2-48); //// 0 的Ascll - 48 else if(hex_char1 >= 'A' && hex_char1 <='F') int_ch2 = hex_char2-55; //// A 的Ascll - 65 else int_ch2 = hex_char2-87; //// a 的Ascll - 97 int_ch = int_ch1+int_ch2; NSLog(@"int_ch=%d",int_ch); bytes[j] = int_ch; ///将转化后的数放入Byte数组里 j++; } NSData *newData = [[NSData alloc] initWithBytes:bytes length:128]; NSLog(@"newData=%@",newData); //3. NSData 与 UIImage //NSData->UIImage UIImage *aimage = [UIImage imageWithData: imageData]; //例:从本地文件沙盒中取图片并转换为NSData NSString *path = [[NSBundle mainBundle] bundlePath]; NSString *name = [NSString stringWithFormat:@"ceshi.png"]; NSString *finalPath = [path stringByAppendingPathComponent:name]; NSData *imageData = [NSData dataWithContentsOfFile: finalPath]; UIImage *aimage = [UIImage imageWithData: imageData]; //UIImage-> NSData NSData *imageData = UIImagePNGRepresentation(aimae);
转载: http://blog.csdn.net/ganlijianstyle/article/details/7990797