Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Array
# Converts hex input to pack("C*") (binary) output. Hex can be any of:
# 
#  [65,65,65].binarize       #=> "AAA" # Decimal notation
#  [0x41,0x41,0x41].binarize #=> "AAA" # Hex notation
#  %w{41 41 41}.binarize     #=> "AAA" # Strings of hex notation
#  [0x41,"41",0x41].binarize #=> "AAA" # Mixed fixnums and strings
#  "414141".binarize         #=> "AAA" # Extension to String, below.
  def binarize
   self.collect {|x| (x.class == String) ? x.to_i(16) : x}.pack("C*")
  end
end

class String
# Converts hex input (string format) to packed output. See Array#binarize.
  def binarize
    self.scan(/../).binarize
  end
end