title: Converting from decimal to hex
body: <p>I want to convert a number to hex and store the result in a <code>[]byte</code> up to length 4.
  Here's what I came up with, but it feels very roundabout.</p>
  
  <pre><code>package main
  
  import (
      "encoding/hex"
      "fmt"
  )
  
  func main() {
  
      hexstring := fmt.Sprintf("%x", 12345678)
      fmt.Println(hexstring)
      hexbytes, _ := hex.DecodeString(hexstring)
      for {
          if len(hexbytes) &gt;= 4 {
              break
          }
          hexbytes = append(hexbytes, 0)
      }
      fmt.Println(hexbytes)
  }
  </code></pre>
  
  <p>I think there must be a better way to do this using <code>make([]byte, 4)</code> and the <code>encoding/binary</code> package, but I couldn't get it to work. </p>
  
  <p>Sandbox link: <a href="http://play.golang.org/p/IDXCatYQXY" rel="nofollow">http://play.golang.org/p/IDXCatYQXY</a></p>