1234567891011121314151617 |
- var cvt []rune = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
- func toHex(num int) string {
- if num == 0 {
- return "0"
- }
- res := make([]rune, 0)
- // Arithmetic shifts for signed integer and logical shifts for unsigned integer.
- for n := uint32(num); n != 0; n >>= 4 {
- res = append(res, cvt[n&0xf])
- }
- for l, r := 0, len(res)-1; l < r; l, r = l+1, r-1 {
- res[l], res[r] = res[r], res[l]
- }
- return string(res)
- }
|