#!/usr/bin/env ruby # vim:ts=2 sw=2 et # charucs.rb : 將所輸入的字轉換成 ucs2/ucs4/utf8/big5 hex. # Edward G.J. Lee (06/13/06) # 2007/08/29 加入 gb2312 require 'iconv' pname = File.basename($0) if ARGV.length == 0 or ARGV[0] =~ /-*[Hh].*/ puts puts "Usage: #{pname} your characters" puts " Use space to seperate each character." puts exit end locale = `echo $LC_ALL`.chomp if locale == "" locale = `echo $LC_CTYPE`.chomp if locale == "" locale = `echo $LANG`.chomp end end enc = locale.split('.') def outPut(a, b) printf("UCS2/4 : 0x%X (%d)\n", a[0], a[0]) size = b.length if size == 1 printf("UTF-8 : 0x%X\n", b[0]) elsif size == 2 printf("UTF-8 : 0x%X%X\n", b[0], b[1]) elsif size == 3 printf("UTF-8 : 0x%X%X%X\n", b[0], b[1], b[2]) elsif size == 4 printf("UTF-8 : 0x%X%X%X%X\n", b[0], b[1], b[2], b[3]) end end def outPutb5(b = ["na"]) puts "Big-5 : 0x" + b[0].to_s.upcase end def outPutgb(g = ["na"]) puts "gb2312 : 0x" + g[0].to_s.upcase end ARGV.each do |s| if locale =~ /UTF/i ucs = s.unpack('U*') b5 = Iconv.new('big-5//IGNORE', 'utf-8').iconv(s).unpack('H*') gb = Iconv.new('gb2312//IGNORE', 'utf-8').iconv(s).unpack('H*') outPut(ucs, s) outPutb5(b5) outPutgb(gb) elsif locale =~ /Big5|BIG5/ u8 = Iconv.new('utf-8', enc[1]).iconv(s) ucs = Iconv.new('utf-8', enc[1]).iconv(s).unpack('U*') b5 = s.unpack('H*') gb = Iconv.new('gb2312//IGNORE', enc[1]).iconv(s).unpack('H*') outPut(ucs, u8) outPutb5(b5) outPutgb(gb) else puts "Please use under UTF-8 or Big5 locale." end end