Add hex encoding function

This was done in enough places to warrant having it only written once,
plus this one won't write an extra zero byte past the end of its buffer,
unlike the sprintf method.

Change-Id: Ie5adabde896838dd955a6da768664ac6108f04d6