|
1 | 1 | package main
|
2 | 2 |
|
3 |
| -func toHex(num int) string { |
4 |
| - if num == 0 { |
| 3 | +// Real Solution |
| 4 | +//---------------------------------------- |
| 5 | + |
| 6 | +// func toHex(num int) string { |
| 7 | +// if num == 0 { |
| 8 | +// return "0" |
| 9 | +// } |
| 10 | + |
| 11 | +// const bitsPerDigit = 4 |
| 12 | +// const mask = (1 << bitsPerDigit) - 1 |
| 13 | + |
| 14 | +// hex := make([]byte, 8) |
| 15 | +// i := 7 |
| 16 | + |
| 17 | +// // Convert negative numbers to their two's complement representation |
| 18 | +// if num < 0 { |
| 19 | +// num = (1 << 32) + num |
| 20 | +// } |
| 21 | + |
| 22 | +// for num != 0 { |
| 23 | +// nib := num & mask |
| 24 | +// hex[i] = getHexDigit(nib) |
| 25 | +// num >>= bitsPerDigit |
| 26 | +// i-- |
| 27 | +// } |
| 28 | + |
| 29 | +// return string(hex[i+1:]) |
| 30 | +// } |
| 31 | + |
| 32 | +// func getHexDigit(n int) byte { |
| 33 | +// if n < 10 { |
| 34 | +// return byte('0' + n) |
| 35 | +// } |
| 36 | +// return byte('a' + n - 10) |
| 37 | +// } |
| 38 | + |
| 39 | +//-------------------------- |
| 40 | + |
| 41 | +// Just for Fun |
| 42 | + |
| 43 | +import ( |
| 44 | + "fmt" |
| 45 | + "math" |
| 46 | +) |
| 47 | + |
| 48 | +func ToHex(n interface{}) (string, error) { |
| 49 | + switch v := n.(type) { |
| 50 | + case int: |
| 51 | + return toHexInt(int32(v)), nil |
| 52 | + case int32: |
| 53 | + return toHexInt(v), nil |
| 54 | + case int64: |
| 55 | + return toHexInt64(v), nil |
| 56 | + case float32: |
| 57 | + return toHexFloat(float64(v)), nil |
| 58 | + case float64: |
| 59 | + return toHexFloat(v), nil |
| 60 | + default: |
| 61 | + return "", fmt.Errorf("Unsupported type: %T", v) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func toHexInt(n int32) string { |
| 66 | + if n == 0 { |
5 | 67 | return "0"
|
6 | 68 | }
|
7 | 69 |
|
8 |
| - const bitsPerDigit = 4 |
9 |
| - const mask = (1 << bitsPerDigit) - 1 |
| 70 | + ref := "0123456789abcdef" |
| 71 | + result := "" |
| 72 | + |
| 73 | + for n != 0 { |
| 74 | + result = string(ref[n&0xF]) + result |
| 75 | + n >>= 4 |
| 76 | + } |
10 | 77 |
|
11 |
| - hex := make([]byte, 8) |
12 |
| - i := 7 |
| 78 | + return result |
| 79 | +} |
13 | 80 |
|
14 |
| - // Convert negative numbers to their two's complement representation |
15 |
| - if num < 0 { |
16 |
| - num = (1 << 32) + num |
| 81 | +func toHexInt64(n int64) string { |
| 82 | + if n == 0 { |
| 83 | + return "0" |
17 | 84 | }
|
18 | 85 |
|
19 |
| - for num != 0 { |
20 |
| - nib := num & mask |
21 |
| - hex[i] = getHexDigit(nib) |
22 |
| - num >>= bitsPerDigit |
23 |
| - i-- |
| 86 | + ref := "0123456789abcdef" |
| 87 | + result := "" |
| 88 | + |
| 89 | + for n != 0 { |
| 90 | + result = string(ref[n&0xF]) + result |
| 91 | + n >>= 4 |
24 | 92 | }
|
25 | 93 |
|
26 |
| - return string(hex[i+1:]) |
| 94 | + return result |
27 | 95 | }
|
28 | 96 |
|
29 |
| -func getHexDigit(n int) byte { |
30 |
| - if n < 10 { |
31 |
| - return byte('0' + n) |
| 97 | +func toHexFloat(n float64) string { |
| 98 | + bits := math.Float64bits(n) |
| 99 | + ref := "0123456789abcdef" |
| 100 | + result := "" |
| 101 | + |
| 102 | + for i := 60; i >= 0; i -= 4 { |
| 103 | + nibble := (bits >> i) & 0xF |
| 104 | + result += string(ref[nibble]) |
32 | 105 | }
|
33 |
| - return byte('a' + n - 10) |
| 106 | + |
| 107 | + return result |
34 | 108 | }
|
35 | 109 |
|
36 |
| -//-------------------------- |
| 110 | +func main() { |
| 111 | + num := 255 |
| 112 | + hexStr, _ := ToHex(num) |
| 113 | + fmt.Println(hexStr) // Output: ff |
| 114 | + |
| 115 | + num32 := int32(65535) |
| 116 | + hexStr32, _ := ToHex(num32) |
| 117 | + fmt.Println(hexStr32) // Output: ffff |
37 | 118 |
|
| 119 | + num64 := int64(4294967295) |
| 120 | + hexStr64, _ := ToHex(num64) |
| 121 | + fmt.Println(hexStr64) // Output: ffffffff |
| 122 | + |
| 123 | + f := float32(3.14) |
| 124 | + hexFloat, _ := ToHex(f) |
| 125 | + fmt.Println(hexFloat) // Output: 4048f5c3 |
| 126 | + |
| 127 | + d := 3.14159 |
| 128 | + hexDouble, _ := ToHex(d) |
| 129 | + fmt.Println(hexDouble) // Output: 400921fb54442d18 |
| 130 | +} |
0 commit comments