#!/usr/bin/env ruby def convert_from_old_encoding(str) begin result = str.encode('windows-1252') result.force_encoding('utf-8') rescue Encoding::UndefinedConversionError result = "" result.force_encoding('binary') str.each_char do |c| c = begin c.encode('windows-1252') rescue c.force_encoding('binary') c[1,1] end c.force_encoding('binary') result += c end result.force_encoding('utf-8') end begin result.match(/.*/) rescue result2 = "" result.force_encoding('binary') while result.length > 0 if result.sub!(/^[\x00-\x7F]+/, '') result2 += $& end if result.sub!(/^([\x80-\xFF]+)/, '') substr = $& substr.force_encoding('utf-8') begin substr.match(/.*/) rescue end end end end result end for line in $stdin.readlines $stdout.write(convert_from_old_encoding(line)) end