#! /usr/local/bin/perl

# This script prints a table of characters in several formats.

$low = ($ARGV[0] ? $ARGV[0] : 0);
$high = ($ARGV[1] ? $ARGV[1] : 255);

@name = ("NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
	"BS", "HT", "NL", "VT", "NP", "CR", "SO", "SI",
	"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
	"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US", "SP");
$name[127] = "DEL";

# print the header
print "Dec\tOct\tHex\tChr\tMaRC\tSym\n";

sub marc
{
	local ($marc) = "_abcdefghijklmnopqrstuvwxyz0123456789";
	local ($n) = (@_);
	if ($n > 0 && $n <= 36) {
		return substr ($marc, $n, 1);
	} elsif ($n > 36 && $n <= 50) {
		return $n;
	}
}

for $i ($low .. $high) {
	printf "%d\t%o\t%X\t", $i, $i, $i;
	if ($i < 32) {
		printf "^%c",  ord('A')+$i-1;
	} else {
		printf "%c", $i;
	}
	print "\t", marc($i);
	if ($name[$i]) {
		print ("\t$name[$i]");
	} else {
		print "\t";
	}
	if ($i >= 0x80) {
		uni2utf8($i);
	}
	print "\n";
}

#for $i (128 .. $high) {
	#printf "%c", $i;
#}
#print "\n";

# http://czyborra.com/utf/
sub uni2utf8
{
	local ($c) = (@_);
	local ($d);
	if ($c < 0x80) { # 0 .. 127
		printf "%d=%c", $c, $c;
	} elsif ($c < 0x800) { # 128 .. 2047
		printf "%d=%c ", $d = (0xC0 | $c>>6), $d;
		printf "%d=%c ", $d = (0x80 | $c & 0x3F), $d;
	} elsif ($c < 0x10000) { # 2048 .. 
		printf "%c", (0xE0 | $c>>12);
		printf "%c", (0x80 | $c>>6 & 0x3F);
		printf "%c", (0x80 | $c & 0x3F);
	} elsif ($c < 0x200000) { # 2,097,152 ..
		printf "%c", (0xF0 | $c>>18);
		printf "%c", (0x80 | $c>>12 & 0x3F);
		printf "%c", (0x80 | $c>>6 & 0x3F);
		printf "%c", (0x80 | $c & 0x3F);
	}
}

