Created
February 3, 2013 05:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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