再投稿
import Foundation

func toHex<T:UnsignedInteger>(_ val:T)->String {
let byteLen=MemoryLayout<T>.size
let fmtStr = [1:"%02hhx",2:"%04hx",4:"%08lx",8:"%016llx"]
return String(format:fmtStr[byteLen]!, val as! CVarArg)
}

print( toHex(UInt8(0x0f)) )
print( toHex(UInt16(0x01ff)) )
print( toHex(UInt32(0x010000ff)) )
print( toHex(UInt64(0x01000000000000ff)) )