Skip to content

Instantly share code, notes, and snippets.

@takeshy
Created April 22, 2015 09:01
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 takeshy/e6a481a7615f73f62b3d to your computer and use it in GitHub Desktop.
Save takeshy/e6a481a7615f73f62b3d to your computer and use it in GitHub Desktop.
HEX文字列をbinaryに変換する
#!/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