Created
August 12, 2017 12:52
-
-
Save kyanny/cef49f0329f0f428573f6ce47947cb55 to your computer and use it in GitHub Desktop.
文字列を \xXX 形式に変換する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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