Skip to content

Instantly share code, notes, and snippets.

@gpittarelli
Created February 3, 2013 05:33
Show Gist options
  • Save gpittarelli/4700650 to your computer and use it in GitHub Desktop.
Save gpittarelli/4700650 to your computer and use it in GitHub Desktop.
Super simple c function to print a binary string without worrying about messing up the terminal
/* Print a binary string `str` of `len` bytes safely by printing
* non-printable characters as red hex codes. */
void safe_print(unsigned char *str, size_t len) {
unsigned char *it = str;
while (len--) {
if (isprint(*it)) {
putchar(*it);
} else {
printf("\33[31m%02X\33[0m", *it);
}
++it;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment