0

I want to NSLog the content of a PDF that has compressed stream objects which include zeros ('0') in the middle of the stream.

Unfortunately the first occurrence of '0' in the first stream object terminates the output on the console...

Couldn't find anything on SO or in the cloud at all.
Tried it in 4 different ways...

    NSString *pdfFilePath = [[NSBundle mainBundle] pathForResource: @"myPDF" ofType: @"pdf"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:pdfFilePath];
    NSData *myData = [NSData dataWithContentsOfURL:fileURL];

    ________________________________________________________________________
    //1
    NSString *string = [[NSString alloc] initWithData:myData
                                             encoding:NSASCIIStringEncoding];
    NSLog(@"%@", string);
    ________________________________________________________________________
    //2   
    NSString *myDataAsString = [[NSString alloc] initWithData:myData
                                                     encoding:NSASCIIStringEncoding];
    NSLog(@"%@", myDataAsString);
    ________________________________________________________________________
    //3
    NSString *myPDFasString = [NSString stringWithContentsOfURL:fileURL
                                                       encoding:NSASCIIStringEncoding error:NULL];
    NSLog(@"%@", myPDFasString);
    ________________________________________________________________________ 
    //4       
    NSString *stringFromFileAtURL = [[NSString alloc] initWithContentsOfURL:fileURL
                                                                   encoding:NSASCIIStringEncoding error:NULL];
    NSLog(@"%@", stringFromFileAtURL);        

OUTPUT on the console for all 4 of them: (of course -- it is always a null-terminated string in the backing-store)

    %PDF-1.1  
    %
    1 0 obj <<  
      /Type /Catalog  
      /Count 1  
      /Pages 6 0 R >>  
    endobj

    2 0 obj <<  
    %%/Type /Stream  
      /Filter /FlateDecode  
      /Length 12 >>  

    stream
    x+TT

Here the stream ends because of the '0' after 'x+TT' (which is the beginning of stream object)...

Anyone?

mramosch
  • 458
  • 4
  • 14
  • 1
    Show some relevant code in your question. – rmaddy Jul 21 '15 at 17:29
  • `myData` can't simply be converted to an `NSString` since `myData` isn't actually string data. – rmaddy Jul 21 '15 at 20:15
  • Yeah, you just get the NSData description... - That's not the point! The problem is not how to retrieve the string to be passed to NSLog. The problem is that the string has a '0' in the middle because it's just a string representation of some binary data which happens to have a '0' in the middle of the data-blip -> and this '0' terminates the stream as if it were (actually it is) the null-terminator. I need a 'String Format Specifier' that treats the whole blip as an entity and doesn't rely on a terminating zero! – mramosch Jul 21 '15 at 20:59

1 Answers1

0

NSLog when formatting a C-style string will stop at a null, as will anything else outputting a C-string.

There are multiple ways you can tackle your problem with simple code, this is only for debugging so no need to get fancy. Here are some pointers:

  • NSData can give you a pointer to the bytes, bytes, and how many there are, length.
  • The standard C library function isprint will tell you if a character is printable.
  • The standard C library function putchar will output a single character to the console.
  • If you want to produce some representation of the non-printables you might find printf useful.

If you get stuck ask another question including the code you've written.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Oh, come on guys - I don't wanna be rude - I know you are trying to help but you have to read my question more carefully!!! Can NSLog write anything to the console that has an occurrence of a '0' in the middle of the output-string (before the string-terminating '0' at the end)? – mramosch Jul 21 '15 at 20:47
  • @mramosch - Apologies that wasn't clear, edited the answer. – CRD Jul 21 '15 at 20:57
  • **@CRD:** Thank you for clarifying - but that's exactly my point and question. Is there either with or without NSLog a chance to get a data-blip that includes a zero in its body - out to the console? Got any ideas on this? – mramosch Jul 21 '15 at 21:12
  • @mramosch - Yes, write some code to do it, the functions mentioned in the answer are all you need. – CRD Jul 21 '15 at 21:16
  • I just can't believe it! 2015 and we should still mess around with this low-level stuff - as if I were the first who wanted to see a binary stream on the console - unbelievable... Thx anyhow, but still waiting for someone to come around and prove you wrong... ;-))) – mramosch Jul 21 '15 at 22:19
  • @mramosch - Seriously? This is a few lines of custom code, just write it. A loop, a conditional, and you're done. – CRD Jul 22 '15 at 05:37