title: Hex String to Byte Array in Ruby
body: <p>I am trying to convert a hex string into byte array using ruby. </p>
  
  <p>48656c6c6f2c20576f726c6421 => 0100 1000 0110 0101 0110 1100 0110 1100 0110 1111 0010 1100 0010 0000 0101 0111 0110 1111 0111 0010 0110 1100 0110 0100 0010 0001 => [72, 65...]</p>
  
  <p>Any suggestions on the best approach to do this?</p>
  
  <p>This is what i have written till now but not that much happy to continue further, wondering there could be a much easier way</p>
  
  <p>binaryArray = Array.new</p>
  
  <pre><code>            hex.each_char do |x|
                       bin = x.hex.to_s(2) #get the binary value for the HEX
                       val = bin.rjust(4,'0') # padding with zeros to have a 4 digits
                       binaryArray.push(val)
              end
  </code></pre>