ios -- 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 *[email protected]"";
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://www.devdiv.com/home.php?mod=space&uid=11511&do=blog&id=8919

---恢复内容结束---

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 *[email protected]"";
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://www.devdiv.com/home.php?mod=space&uid=11511&do=blog&id=8919

时间: 2024-10-11 06:00:47

ios -- NSdata 与 NSString,Byte数组,UIImage 的相互转换的相关文章

ios -- NSdata 与 NSString,Byte数组,UIImage 的相互转换(转)

1. NSData 与 NSStringNSData-> NSStringNSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding]; NSString->NSDataNSString *aString = @"1234abcd";NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding]

NSdata 与 NSString,Byte数组,UIImage 的相互转换

1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding]; NSString->NSData NSString *aString = @"1234abcd"; NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncodi

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: NSUTF8StringEncodin

Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage

1,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding]; NSString --> NSData NSString *aString = @"1234"; NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncod

iOS解决NSData转NSString后字符为空

iOS中,将NSData转NSString的一般方法为[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];但是当data中包含00时,所获得的新字符就会为nil,这时我们应该这样转 [NSString stringWithUTF8String:[data bytes]];

iOS 7: Base64 Encode and Decode NSData and NSString Objects

iOS 7: Base64 Encode and Decode NSData and NSString Objects FRI, JAN 24 CORE SERVICES TWEET With the release of iOS 7, Apple added support for encoding and decoding data using Base64. In this post we will walk through two examples using Base64 to enc

byte数组与对象之间的相互转换

在进行网络通信时可能需要传输对象,如果用NIO的话,只能用Bytebuffer和channel直接 通过ByteArray*Stream和Object*Stream可以将byte数组和对象进行相互的转换. 1.byte数组转对象: byte [] data=initData();//初始化byte数组 ByteArrayInputStream inputStream=new ByteArrayInputStream(data); ObjectInputStream oInputStream=ne

[C#参考]byte数组和Image的相互转换

功能需求 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组在内存中操作. 2.把内存中的byte数组转换成Image对象,赋值给相应的控件显示. 3.从图片byte数组得到对应的图片格式,生成一张图片保存到磁盘中. 这个的Image是System.Drawing.Image. //Get an image from file Image image = Image.FromFile("D:\\test.jpg"); Bitmap bitmap = new B

Delphi Byte数组与Int String之间的相互转换

http://www.cnblogs.com/lcw/p/3352864.html string string = AnsiString = 长字符串,理论上长度不受限制,但其实受限于最大寻址范围2的32次方=4G字节: 变量Str名字是一个指针,指向位于堆内存的字符序列,字符序列起始于@Str[1],@Str[1]偏移负16个字节的空间存储着字串长度.引用计数等信息.字符序列以NULL结束. string[n] string[n] = ShortString = 短字符串,最多容纳255个字符