#!/usr/bin/perl # Find optimal encoding key for shindou7_no_ucase.c @data = ( 0xf0, 0xf7, 0x80, 0xbf, 0x80, 0xbf, 0x80, 0xbf, 0xe0, 0xef, 0x80, 0xbf, 0x80, 0xbf, 0xc0, 0xdf, 0x80, 0xbf, 0x81, 0x9f, 0x40, 0xfc, 0xe0, 0xef, 0x40, 0xfc, 0xa1, 0xa8, 0xa1, 0xfe, 0xad, 0xad, 0xa1, 0xfe, 0xb0, 0xf4, 0xa1, 0xfe, 0xf9, 0xfc, 0xa1, 0xfe, 0x8e, 0x8e, 0xa1, 0xfe, 0x8f, 0x8f, 0xa1, 0xfe, 0xa1, 0xfe, ); $best = 0xffffff; $key = 0; for($i = 0; $i < 256; $i++) { $char = $nonchar = $space = 0; foreach $j (@data) { $enc = $j ^ $i; if( $enc == 32 ) { $space++; } elsif( $enc > 32 && $enc < 127 ) { $char++; if( chr($enc) eq '\\' || chr($enc) eq '\"' ) { $char++; } } else { $nonchar += 4; } } print "key = $i, space = $space, char = $char, nonchar = $nonchar\n"; if( $char + $nonchar < $best ) { $key = $i; $best = $char + $nonchar; } } print "-> key = $key\n"; foreach $i (@data) { $enc = $i ^ $key; printf '%02x ^ %d = %02x ', $i, $key, $enc; if( $enc >= 32 && $enc < 127 ) { print chr($enc); } else { printf '\%o', $enc; } print "\n"; }