I'm relatively new to C. I have a binary buffer that I want to print to stdout, like so:
unsigned char buffer[1024];
Is there a way to use printf to output this to stdout? I took a look at the man page for printf, however there is no mention of an unsigned char*, only of const char* for %s.
I suppose I could loop over the buffer and do putchar(buffer[i]), however I think that would be rather inefficient. Is there a way to do this using printf? Should I use something like fwrite instead, would that be more appropriate?
Thanks.
edit: Sorry for the confusion, I'm not looking to convert anything to hexadecimal/binary notation, just write out the buffer as-is. For example, if I had [0x41 0x42 0x42 0x41] in the buffer then I would want to print "ABBA".
fwriteis the way to go.printfwith a binary string might prove to be a mistake if you use%s, since%swill terminate on the first NUL character, even though NUL is often a valid value in these circumstances.write(1, buffer, sizeof(buffer))(1 is the file descriptor of stdout). (Don't do that if you're doing otherstdiowrites tostdout, though.)fwriteis the counterpart tofprintffor raw binary data. They are both C library functions.writeOTOH is the low level OS system call that is used to implementfwrite(andfprintffor that matter).fwrite(andfprintf) are buffered,writeis not. I am not sure ifwriteis portable, butfwriteis.