Skip to content

Instantly share code, notes, and snippets.

@kyanny
Created August 12, 2017 12:53
Show Gist options
  • Save kyanny/b3f19fb967273b3237ec58f5eec1fa6b to your computer and use it in GitHub Desktop.
Save kyanny/b3f19fb967273b3237ec58f5eec1fa6b to your computer and use it in GitHub Desktop.
\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