美文网首页
各种数据类型之间的转换

各种数据类型之间的转换

作者: 苏宇lovecc | 来源:发表于2016-08-29 17:29 被阅读27次
    1. NSData-> NSString
    NSString *aString 
     = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
    
    1. NSString->NSData
    NSString *aString = @"1234abcd";
    NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
    
    1. 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]);
    }
    
    1. 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];
    
    1. 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];
    
    1. UIImage-> NSData
    UIImage *aimage = [UIImage imageWithData: imageData];
    

    相关文章

      网友评论

          本文标题:各种数据类型之间的转换

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