💾 Archived View for thrig.me › blog › 2024 › 03 › 01 › hasnul.c captured on 2024-08-31 at 14:05:04.

View Raw

More Information

⬅️ Previous capture (2024-03-21)

-=-=-=-=-=-=-

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

char msg[] = {'H', 'e', 'l', 'l', '\0', ' ', 'W', 'o', 'r', 'l', 'd'};

int
main(void)
{
	printf("%s\n", msg);
	size_t len = sizeof(msg) / sizeof(char);
	printf("%.*s (%zu)\n", (int) len, msg, len);
	for (size_t i = 0; i < len; ++i) {
		int ch = msg[i];
		if (isprint(ch)) {
			putchar(ch);
		} else if (ch < 256) {
			printf("\\x%02x", ch);
		} else {
			// TODO support bigger ints (such as used by
			// ncurses that have chtype attributes on them,
			// or probably other cases
			abort();
		}
	}
	putchar('\n');
}