Unicode エスケープシーケンスをRubyで扱う方法。 module Unicode def escape(str) ary = str.unpack("U*").map!{|i| "\\u#{i.to_s(16)}"} ary.join end UNESCAPE_WORKER_ARRAY = [] def unescape(str) str.gsub(/\\u([0-9a-f]{4})/) { UNESCAPE_WORKER_ARRAY[0] = $1.hex UNESCAPE_WORKER_ARRAY.pack("U") } end module_function :escape, :unescape end require "kconv" utf8_string = "こんにちは".toutf8 escape_string = Unicode.escape(utf8_string