#!/usr/bin/perl # # Written by Marc Liyanage (http://www.entropy.ch) # print iso2mac(utf82iso(join("", <>))); sub iso2mac { my ($string) = @_; $string =~ tr/\xa0\xa1\xa4\xa5\xa7\xa8\xaa\xab\xac\xae\xaf\xb0\xb4\xb6\xb7\xb8\xba\xbb\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd6\xd8\xd9\xda\xdb\xdc\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xff/\xca\xc1\xdb\xb4\xa4\xac\xbb\xc7\xc2\xa8\xf8\xa1\xab\xa6\xe1\xfc\xbc\xc8\xc0\xcb\xe7\xe5\xcc\x80\x81\xae\x82\xe9\x83\xe6\xe8\xed\xea\xeb\xec\x84\xf1\xee\xef\x85\xaf\xf4\xf2\xf3\x86\xa7\x88\x87\x89\x8b\x8a\x8c\xbe\x8d\x8f\x8e\x90\x91\x93\x92\x94\x95\x96\x98\x97\x99\x9b\x9a\xd6\xbf\x9d\x9c\x9e\x9f\xd8/; return $string; } sub utf82iso { my ($string) = @_; my %cache = (); # Match 2-byte sequences with the first byte having # the high bits set to 110xxxxx (0xc0 - 0xdf) and the second byte having # the high bits set to 10xxxxxx (0x80 - 0xbf). Send these sequences to the # decode function and replace them with the result of the function. # $string =~ s/([\xc0-\xdf][\x80-\xbf])/$cache{$1} ||= decode_2byte_utf8($1)/eg; return $string; } sub decode_2byte_utf8 { # Split the incoming 2-character string into two separate # strings each containing only one character # my ($a, $b) = split(//, $_[0]); # Get the integer values of the two characters using the ord() function. # Then mask their UTF-8 high bit patterns (3 bits / 0x1f for the first byte, # 2 bits / 0x3f for the second) to get the 11-bit character number which # is now still spread over two bytes. # Shift the first byte to the left by 6 positions # and OR the two bytes together. Then take the rightmost # 8 bits (& 0xff), interpret them as character with the chr() # function and return that. # return chr((((ord($a) & 0x1f) << 6) | (ord($b) & 0x3f) & 0xff)); }