user979331
user979331

Reputation: 11841

Objective-C Convert byte[] to NSData

I have this byte[] data from a webservice that looks like this (this is just a small porton of it):

JVBERi0xLjQgU2hhcnAgU2Nhbm5lZCBJbWFnZVBERgolU2hhcnAgTm9uLUVuY3J5cHRpb24KMyAwIG9iago8PAovVHlwZSAvUGFnZQovUGFyZW50IDEgMCBSCi9SZXNvdXJjZXMgNCAwIFIKL0NvbnRlbnRzIDUgMCBSCi9NZWRpYUJveCBbMCAwIDYxMyA3OTBdCj4+CmVuZG9iago0IDAgb2JqCjw8Ci9Qcm9jU2V0IFsvUERGIC9JbWFnZUJdCi9YT2JqZWN0IDw8IC9JbWcxIDYgMCBSID4+Cj4+CmVuZG9iago1IDAgb2JqCjw8Ci9MZW5ndGggMjcKPj4Kc3RyZWFtCjYxMyAwIDAgNzkwIDAgMCBjbQovSW1nMSBEbwplbmRzdHJlYW0KZW5kb2JqCjYgMCBvYmoKPDwKL1R5cGUgL1hPYmplY3QKL1N1YnR5cGUgL0ltYWdlCi9OYW1lIC9JbWcxCi9GaWx0ZXIgL0NDSVRURmF4RGVjb2RlCi9EZWNvZGVQYXJtcyA8PCAgL0sgLTEgL0NvbHVtbnMgMTcwNCAvUm93cyAyMTk2ID4+Ci9XaWR0aCAxNzA0Ci9IZWlnaHQgMjE5NgovQml0c1BlckNvbXBvbmVudCAxCi9Db2xvclNwYWNlIC9EZXZpY2VHcmF5Ci9MZW5ndGggNyAwIFIKPj4Kc3RyZWFtCvywYWH//////////////////////////X3////////////r5bIVv/Zbqq/D9fH//////////X//ymQ1vH/////X/f///69/9f////+/+vfr//+//9f/79e//9ZZmL/XG//9f/7////X/17////+vde///a/+V+RGlpyuOgQITiPMjojo8gQJTTI

What I am trying to do is put this byte[] data into an NSData variable so I can save this PDF file and display is on my app.

This is what I have tried:

 NSArray *byteArray = [dataSource.areaData GetPDFFileData:[NSString stringWithFormat:@"%@",@""]];

        unsigned c = byteArray.count;
        uint8_t *bytes = malloc(sizeof(*bytes) * c);

        unsigned i;
        for (i = 0; i < c; i++)
        {
            NSString *str = [byteArray objectAtIndex:i];
            int byte = [str intValue];
            bytes[i] = (uint8_t)byte;
        }

        NSData* data = [NSData dataWithBytes:(const void *)bytes length:sizeof(unsigned char)*c];

but I get this error

-[__NSCFDictionary intValue]: unrecognized selector sent to instance 0x17eb28e0

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary intValue]: unrecognized selector sent to instance 0x17eb28e0'

on this line

int byte = [str intValue];

How do I fix this? is there something wrong with my byte[] data or the way I am converting it?

Below is an image of what is in byteArray

enter image description here

Here is my call to get the byte[] from the webservice

-(NSArray *)GetPDFFileData:(NSString *)y
{
    NSString *FileBrowserRequestString = [NSString stringWithFormat:@"%@?y=%@",kIP,y];
    NSURL *JSONURL = [NSURL URLWithString:FileBrowserRequestString];
    NSURLResponse* response = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    if(data == nil)
        return nil;
    NSError *myError;
    NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
    return tableArray;
}

Upvotes: 1

Views: 1689

Answers (1)

trojanfoe
trojanfoe

Reputation: 122381

You have an array of dictionary objects where the "data" element contains what looks like a base-64 encoded byte array.

// Untested
NSArray *array = [dataSource.areaData GetPDFFileData:@""];
for (NSDictionary *dict in array) {
    NSString *base64 = dict[@"data"];
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64
                                                       options:0];
    // Do something with data?
}

Upvotes: 1

Related Questions