title: Convert string to hexadecimal in Ruby
body: <p>I'm trying to convert a Binary file to Hexadecimal using Ruby.</p>
  
  <p>At the moment I have the following:</p>
  
  <pre><code>File.open(out_name, 'w') do |f|
    f.puts "const unsigned int modFileSize = #{data.length};"
    f.puts "const char modFile[] = {"
    first_line = true
    data.bytes.each_slice(15) do |a|
      line = a.map { |b| ",#{b}" }.join
      if first_line
        f.puts line[1..-1]
      else
        f.puts line
      end
      first_line = false
    end
    f.puts "};"
  end
  </code></pre>
  
  <p>This is what the following code is generating:</p>
  
  <pre><code>const unsigned int modFileSize = 82946;
  const char modFile[] = {
   116, 114, 97, 98, 97, 108, 97, 115, 104, 0, 0, 0, 0, 0, 0
  , 0, 0, 0, 0, 0, 62, 62, 62, 110, 117, 107, 101, 32, 111, 102
  , 32, 97, 110, 97, 114, 99, 104, 121, 60, 60, 60, 8, 8, 130, 0
  };
  </code></pre>
  
  <p>What I need is the following:</p>
  
  <pre><code>const unsigned int modFileSize = 82946;
  const char modFile[] = {
   0x74, 0x72, etc, etc
  };
  </code></pre>
  
  <p>So I need to be able to convert a string to its hexadecimal value.</p>
  
  <p><code>"116" =&gt; "0x74"</code>, etc</p>
  
  <p>Thanks in advance.</p>