标签:nsdata objective-c ios开发 数据
简介:
1. NSData 与 NSString
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];
NSString->NSData
NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
//将data类型的数据,转成UTF8的数据
+(NSString *)dataToUTF8String:(NSData *)data
{
NSString *buf = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [buf autorelease];
}
//将string转换为指定编码
+(NSString *)changeDataToEncodinString:(NSData *)data encodin:(NSStringEncoding )encodin{
NSString *buf = [[[NSString alloc] initWithData:data encoding:encodin] autorelease];
return buf;
}
2. 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];
3.NSData与NSArray NSDictionary
+(NSString *)getLocalFilePath:(NSString *) fileName
{
return [NSString stringWithFormat:@"%@/%@%@", NSHomeDirectory(),@“Documents”,fileName];
}
包括将NSData写进Documents目录
从Documents目录读取数据
在进行网络数据通信的时候,经常会遇到NSData类型的数据。在该数据是dictionary结构的情况下,系统没有提供现成的转换成NSDictionary的方法,为此可以通过Category对NSDictionary进行扩展,以支持从NSData到NSDictionary的转换。声明和实现如下:
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data {
CFPropertyListRef list = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, kCFPropertyListImmutable, NULL);
if(list == nil) return nil;
if ([(id)list isKindOfClass:[NSDictionary class]]) {
return [(NSDictionary *)list autorelease];
}
else {
CFRelease(list);
return nil;
}
}
NSDate
//函数作用 :date根据formatter转换成string
+(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatter];
return[dateFormatter stringFromDate:date];
}
//函数作用 :string类型的时间戳转换成时间
+(NSString*)dateStringToString:(NSString *)dateStr
{
if ([dateStr isKindOfClass:[NSNull class]]) {
return nil;
}
else{
NSInteger time = [dateStr intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [NSString stringWithFormat:@"%@",date];
NSRange rang;
rang.location = 0;
rang.length = 10;
NSString *needtime = [timestr substringWithRange:rang];
return needtime;
}
}
//函数作用 :将日期从原格式转换成需要的格式
+(NSString*)convertDateFormatter:(NSString*)sourceFormatter
targetFormatter:(NSString*)targetFormatter
dateString:(NSString*)dateString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:sourceFormatter];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:targetFormatter];
return[dateFormatter stringFromDate:date];
}
//函数作用 :将日期字符串转换成date
+(NSDate *)stringToDate:(NSString *)formatter dateString:(NSString *)dateString{
NSDateFormatter *dateFormatter= [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatter];
return [dateFormatter dateFromString:dateString];
}
//时间戳转换成时间
NSInteger time = [[dic objectForKey:@"press_date"] intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [NSString stringWithFormat:@"%@",date];
NSRange rang;
rang.location = 0;
rang.length = 10;
NSString *needtime = [timestr substringWithRange:rang];
1. NSData 与 NSString
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adataencoding: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);
//将高位字节转换为int
-(int )hBytesToInt:(Byte[]) b
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[i] >= 0)
{
s = s + b[i];
} else
{
s = s +256 + b[i];
}
s = s * 256;
if (b[3] >= 0)
{
s = s + b[3];
} else
{
s = s + 256 + b[3];
}
}
return s;
}
//将低位字节转换为int
在网络上传输的是以低位字节进行的; htonl,将int转换为网络字节序的int,ntohl,将网络字节序的int转换回来,-(int) lBytesToInt:(Byte[]) b,将以低位传输的网络字节序bytes数组转换为htonl之后对应的值
-(int) lBytesToInt:(Byte[]) b
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[3-i] >= 0)
{
s = s + b[3-i];
} else
{
s = s + 256 + b[3-i];
}
s = s * 256;
}
if (b[0] >= 0)
{
s = s + b[0];
} else {
s = s + 256 + b[0];
}
return s;
}
标签:nsdata objective-c ios开发 数据
原文地址:http://blog.csdn.net/u011374880/article/details/41356143