Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
\xXX 形式の文字列を UTF-8 とみなしてデコードする
#!/usr/bin/env ruby
def x2str(x)
# assume x is hex format of UTF-8 string, so that process 3 bytes at once
x.gsub(/\\x(..)\\x(..)\\x(..)/) {
["#{$1}#{$2}#{$3}"].pack("H*")
}
end
x = ARGV[0]
unless x
puts <<USAGE
Usage: ruby #{$0} "\xE6\x9C\xAC"
USAGE
end
puts x2str(x)
# ruby x2str.rb "\xE6\x9C\xAC\xE6\x97\xA5\xE3\x81\xAF\xE6\x99\xB4\xE5\xA4\xA9\xE3\x81\xAA\xE3\x82\x8A"
# => 本日は晴天なり
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment