title: How to create a generic integer-to-hex function for all Integer types?
body: <p>I want to create an Integer-to-Hex function for all integer types.</p>
  
  <p>For 1-byte Int8, it returns two letters, eg 0A</p>
  
  <p>For 2-byte Int16, it returns four letters, eg 0A0B</p>
  
  <p>for 8-byte Int64, it returns 16 letters, eg, 0102030405060708</p>
  
  <pre><code>func hex(v: Int) -&gt; String {
      var s = ""
      var i = v
      for _ in 0..&amp;lt;sizeof(Int)*2 {
          s = String(format: "%x", i &amp; 0xF) + s
          i = i &gt;&gt; 4
      }
      return s
  }
  
  func hex(v: Int64) -&gt; String {
      var s = ""
      var i = v
      for _ in 0..&amp;lt;sizeof(Int64)*2 {
          s = String(format: "%x", i &amp; 0xF) + s
          i = i &gt;&gt; 4
      }
      return s
  }
  
  func hex(v: Int32) -&gt; String {
      var s = ""
      var i = v
      for _ in 0..&amp;lt;sizeof(Int32)*2 {
          s = String(format: "%x", i &amp; 0xF) + s
          i = i &gt;&gt; 4
      }
      return s
  }
  
  func hex(v: Int16) -&gt; String {
      var s = ""
      var i = v
      for _ in 0..&amp;lt;sizeof(Int16)*2 {
          s = String(format: "%x", i &amp; 0xF) + s
          i = i &gt;&gt; 4
      }
      return s
  }
  
  func hex(v: Int8) -&gt; String {
      var s = ""
      var i = v
      for _ in 0..&amp;lt;sizeof(Int8)*2 {
          s = String(format: "%x", i &amp; 0xF) + s
          i = i &gt;&gt; 4
      }
      return s
  }
  </code></pre>
  
  <p>The above code works fine.</p>
  
  <p>I then tried to create a generic version like this:</p>
  
  <pre><code>func hex&lt;T: IntegerType&gt;(v: T) -&gt; String {
      var s = ""
      var i = v
      for _ in 0..&lt;sizeof(T)*2 {
          s = String(format: "%x", i &amp; 0xF) + s
          i = i &gt;&gt; 4
      }
      return s
  }
  </code></pre>
  
  <p>When compiling this code, I got the error: T is not convertible to Int</p>
  
  <p>What is the correct way to achieve this task?</p>