Note that there are some explanatory texts on larger screens.

plurals
  1. POData corrupt after converting byte[] to NSData
    primarykey
    data
    text
    <p>I have .Net web service response containing a byte[] entry, among other fields. The data is a PDF file.</p> <p>I extract the Dictionary from the received data with: [NSJSONSerialization JSONObjectWithData]</p> <p>Hereafter I use the following code to convert the byte[] to NSData. I then save the result to disk (see last line).</p> <p>When opening the resulting PDF file, I get the following error:</p> <p><strong>"failed to find PDF header: `%PDF' not found."</strong></p> <pre><code> NSArray *byteArray = [rootDictionary objectForKey:@"file"]; unsigned c = byteArray.count; uint8_t *bytes = malloc(sizeof(*bytes) * c); unsigned i; for (i = 0; i &lt; c; i++) { NSString *str = [byteArray objectAtIndex:i]; int byte = [str intValue]; bytes[i] = (uint8_t)byte; } NSData* data = [NSData dataWithBytes:(const void *)byteArray length:sizeof(unsigned char)*c]; //Save to disk using svc class. NSString *localPath = [svc saveReport:data ToFile:[rootDictionary objectForKey:@"name"]]; </code></pre> <p>I also tried converting the byte[] to a base64 NSString (on the service side) and then back to NSData in my app, which worked (**mostly) but I was told that it's sloppy code.</p> <p>** When pulling multiple PDF asynchronously at the same time, some of these reports received as base64 strings were also corrupted.</p> <p>PS. Please let me know if I must supply the code from my svc class as well, but I don't think the problem is there.</p> <p><strong>Edit:</strong> I created a new web service method which takes a byte[] as input, then modified my iOS app to send the byteArray variable back to the service, where it get's saved to a file. The resulting PDF file is a valid file readable by Adobe. Meaning there is no corruption during transfer.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload