Skip to content

Instantly share code, notes, and snippets.

@kyanny
Created August 12, 2017 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyanny/cef49f0329f0f428573f6ce47947cb55 to your computer and use it in GitHub Desktop.
Save kyanny/cef49f0329f0f428573f6ce47947cb55 to your computer and use it in GitHub Desktop.
文字列を \xXX 形式に変換する
#!/usr/bin/env ruby
def str2x(str)
str.unpack("H*").first.scan(/[0-9a-z]{2}/).map{ |x| '\x' + x.upcase }.join
end
str = ARGV[0]
unless str
puts <<USAGE
Usage: ruby #{$0} STRING
USAGE
end
puts str2x(str)
# ruby str2x.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