#include "wtf8.h" /* * Check if first two bits of the character are "10", meaning it's a UTF-8 * continuation character */ int is_utf8_cont(unsigned char c) { return (c & 0xC0) == 0x80; } /* * Print each octet of a string of characters as lowercase hex followed by a * trailing space, ending with a newline */ void print_octets(char *s) { unsigned char c; /* * Iterate through the string, printing each octet, ending with a newline */ while ((c = *s++)) printf("%c%02x", (is_utf8_cont(c) ? '-' : ' '), c); putchar('\n'); return; } /* * Print each of the UTF-8 characters to align with the output of * print_octets(), with each character in line with the end of the octet that * terminates it, ending with a newline */ void print_characters(char *s) { /* * We need a short counter to find how long each character is */ int c; /* * Iterate through the string */ while (*s) { /* * Print blanks and increment a counter until we find how long this * character is */ for (c = 1; is_utf8_cont(s[c]); c++) printf(" "); /* * Print two spaces, and then the full character */ printf(" "); while (c--) putchar(*s++); } /* * End with a newline */ putchar('\n'); return; } /* * Main function */ int main(int argc, char **argv) { /* * Check we have one and only one argument */ if (argc != 2) exit(EXIT_FAILURE); /* * Print the sole argument first as hex octets, then as characters, spaced * accordingly */ print_octets(argv[1]); print_characters(argv[1]); /* * Done! */ exit(EXIT_SUCCESS); }