1

When the need arises to print to the console the contents of a C byte array (not ASCII), ‘printf’ has the advantage, when stepping through the array, that it does not put a return after each use. Thus the array is printed concisely across the screen. However, it often takes an age for the printout actually to reach the screen and in desperation, I sometimes open or close ‘Terminal’ as this seems to flush out the printout (‘fflush’ does not).

‘NSLog’, on the other hand, prints quickly but one often sees here and elsewhere that it must be applied with each step through the array. Unfortunately, this inserts a return after each use, causing even a short array to cover the page and making it difficult to read or copy.

For the record, I thought it worth pointing out that there is a simple solution, using ‘NSMutableString’ and ‘appendFormat’, that prints across the page. Here is the ‘bells and whistles’ version with commas, spaces and enclosing braces that I keep handy for testing purposes. It is constructed as a C function and prints in decimal (%d); for other formats see Apple’s ‘String Programming Guide’ at https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/introStrings.html.

void byteArrayLog(unsigned char *bArray, int numBytes)
{
    int i;  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSMutableString* dString = [NSMutableString stringWithCapacity:5*numBytes];//Create a mutable string. Note the length.
    [dString appendString:@"{"];//Start with "{".
    for(i = 0; i < numBytes-1; i++)
        [dString appendFormat:@"%d, ", bArray[i]];//Format a byte, follow with a comma and a space.
    [dString appendFormat:@"%d}", bArray[i]];//Format the final byte, finish with a "}".

    NSLog(@"Array contents = %@", dString);//Print to the console.

    [pool drain];//Clean up.
}
15
  • 3
    NSLog() prints to stderr, whereas printf() prints to stdout. It seems that the standard output is not flushed but the standard error stream is - try using fprintf(stderr, "format string: %d", array[index]); in order to print to stderr using a printf()-style function. Commented Nov 16, 2012 at 17:45
  • asl is apple's syslog, introduced in ios4/osx 10.6 :) man asl_search. it has a nice api for querying old logs Commented Nov 16, 2012 at 17:54
  • @Daij-Djan syslog grabs data from standard error. Commented Nov 16, 2012 at 17:56
  • ok :) didnt know :) thanks. :) only knew printf didnt show up in asl.. know I now :D Commented Nov 16, 2012 at 17:59
  • Good suggestion @H2CO3. At first glance it seems to work. With 'fflush' not working I was deflected from exploring that route. Hopefully, the post will be useful in other contexts as this type of dilemma seems to arise often in posts. For NSBum, forestalling questions is also occasionally useful:-) Commented Nov 16, 2012 at 18:14

1 Answer 1

1

NSLog() prints to the standard error stream (stderr), whereas printf() prints to the standard output (stdout). It seems that the standard output is not flushed properly but the standard error stream is - try using

fprintf(stderr, "format string: %d", array[index]);

in order to print to stderr using a printf()-style function.

Sign up to request clarification or add additional context in comments.

1 Comment

This works nicely as one steps through short arrays (~10 bytes) but for longer ones (~100 bytes and more) it partitions the data; however, nowhere near as much as 'NSLog'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.