Created
April 22, 2015 09:01
-
-
Save takeshy/e6a481a7615f73f62b3d to your computer and use it in GitHub Desktop.
HEX文字列をbinaryに変換する
This file contains hidden or 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
#!/bin/env ruby | |
$HEX = false | |
if ARGV[0] == "-x" | |
$HEX = true | |
ARGV.shift | |
end | |
if ARGV.length != 1 | |
puts "#{$0} outputfile" | |
exit -1 | |
end | |
unless file = File.open(ARGV[0],"wb") | |
puts "#{$0} outputfile" | |
puts "#{ARGV[0]} can't write" | |
exit -1 | |
end | |
begin | |
datas = [] | |
if $HEX | |
STDIN.each do |line| | |
datas.concat(line.scan(/[\da-zA-Z]{2}/).map do|i| i.hex end) | |
end | |
else | |
STDIN.each do |line| | |
datas.concat(line.scan(/[\d]{1,3}/).map do|i| i.to_i end) | |
end | |
end | |
file.write(datas.pack("C*")) | |
ensure | |
file.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment