服务器之家

服务器之家 > 正文

golang 将[]byte转成16进制的实现

时间:2021-03-04 00:51     来源/作者:-缘故-

将[]byte转成16进制

?
1
2
3
4
import "crypto/md5"
import "fmt"
sign := md5.Sum([]byte("date string"))
signStr := fmt.Sprintf("%x", sign) //将[]byte转成16进制

补充:golang []byte存储存储的16进制转10进制

项目中有用[]byte存储16进制需要转到10进制,如果用系统自带的函数处理,需要先将[]byte通过hex.EncodeToString转为string,再用strconv.ParseInt转到10进制,过程中由于两次转换导致内存分配过多(每秒执行近100w次),GC出现错误导致进程挂掉。

自己写了一个函数来转,主要是避免出现内存泄漏:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var b2m_map map[byte]uint64 = map[byte]uint64{
    0x00: 0,
    0x01: 1,
    0x02: 2,
    0x03: 3,
    0x04: 4,
    0x05: 5,
    0x06: 6,
    0x07: 7,
    0x08: 8,
    0x09: 9,
    0x0A: 10,
    0x0B: 11,
    0x0C: 12,
    0x0D: 13,
    0x0E: 14,
    0x0F: 15,
    0x10: 16,
    0x11: 17,
    0x12: 18,
    0x13: 19,
    0x14: 20,
    0x15: 21,
    0x16: 22,
    0x17: 23,
    0x18: 24,
    0x19: 25,
    0x1A: 26,
    0x1B: 27,
    0x1C: 28,
    0x1D: 29,
    0x1E: 30,
    0x1F: 31,
    0x20: 32,
    0x21: 33,
    0x22: 34,
    0x23: 35,
    0x24: 36,
    0x25: 37,
    0x26: 38,
    0x27: 39,
    0x28: 40,
    0x29: 41,
    0x2A: 42,
    0x2B: 43,
    0x2C: 44,
    0x2D: 45,
    0x2E: 46,
    0x2F: 47,
    0x30: 48,
    0x31: 49,
    0x32: 50,
    0x33: 51,
    0x34: 52,
    0x35: 53,
    0x36: 54,
    0x37: 55,
    0x38: 56,
    0x39: 57,
    0x3A: 58,
    0x3B: 59,
    0x3C: 60,
    0x3D: 61,
    0x3E: 62,
    0x3F: 63,
    0x40: 64,
    0x41: 65,
    0x42: 66,
    0x43: 67,
    0x44: 68,
    0x45: 69,
    0x46: 70,
    0x47: 71,
    0x48: 72,
    0x49: 73,
    0x4A: 74,
    0x4B: 75,
    0x4C: 76,
    0x4D: 77,
    0x4E: 78,
    0x4F: 79,
    0x50: 80,
    0x51: 81,
    0x52: 82,
    0x53: 83,
    0x54: 84,
    0x55: 85,
    0x56: 86,
    0x57: 87,
    0x58: 88,
    0x59: 89,
    0x5A: 90,
    0x5B: 91,
    0x5C: 92,
    0x5D: 93,
    0x5E: 94,
    0x5F: 95,
    0x60: 96,
    0x61: 97,
    0x62: 98,
    0x63: 99,
    0x64: 100,
    0x65: 101,
    0x66: 102,
    0x67: 103,
    0x68: 104,
    0x69: 105,
    0x6A: 106,
    0x6B: 107,
    0x6C: 108,
    0x6D: 109,
    0x6E: 110,
    0x6F: 111,
    0x70: 112,
    0x71: 113,
    0x72: 114,
    0x73: 115,
    0x74: 116,
    0x75: 117,
    0x76: 118,
    0x77: 119,
    0x78: 120,
    0x79: 121,
    0x7A: 122,
    0x7B: 123,
    0x7C: 124,
    0x7D: 125,
    0x7E: 126,
    0x7F: 127,
    0x80: 128,
    0x81: 129,
    0x82: 130,
    0x83: 131,
    0x84: 132,
    0x85: 133,
    0x86: 134,
    0x87: 135,
    0x88: 136,
    0x89: 137,
    0x8A: 138,
    0x8B: 139,
    0x8C: 140,
    0x8D: 141,
    0x8E: 142,
    0x8F: 143,
    0x90: 144,
    0x91: 145,
    0x92: 146,
    0x93: 147,
    0x94: 148,
    0x95: 149,
    0x96: 150,
    0x97: 151,
    0x98: 152,
    0x99: 153,
    0x9A: 154,
    0x9B: 155,
    0x9C: 156,
    0x9D: 157,
    0x9E: 158,
    0x9F: 159,
    0xA0: 160,
    0xA1: 161,
    0xA2: 162,
    0xA3: 163,
    0xA4: 164,
    0xA5: 165,
    0xA6: 166,
    0xA7: 167,
    0xA8: 168,
    0xA9: 169,
    0xAA: 170,
    0xAB: 171,
    0xAC: 172,
    0xAD: 173,
    0xAE: 174,
    0xAF: 175,
    0xB0: 176,
    0xB1: 177,
    0xB2: 178,
    0xB3: 179,
    0xB4: 180,
    0xB5: 181,
    0xB6: 182,
    0xB7: 183,
    0xB8: 184,
    0xB9: 185,
    0xBA: 186,
    0xBB: 187,
    0xBC: 188,
    0xBD: 189,
    0xBE: 190,
    0xBF: 191,
    0xC0: 192,
    0xC1: 193,
    0xC2: 194,
    0xC3: 195,
    0xC4: 196,
    0xC5: 197,
    0xC6: 198,
    0xC7: 199,
    0xC8: 200,
    0xC9: 201,
    0xCA: 202,
    0xCB: 203,
    0xCC: 204,
    0xCD: 205,
    0xCE: 206,
    0xCF: 207,
    0xD0: 208,
    0xD1: 209,
    0xD2: 210,
    0xD3: 211,
    0xD4: 212,
    0xD5: 213,
    0xD6: 214,
    0xD7: 215,
    0xD8: 216,
    0xD9: 217,
    0xDA: 218,
    0xDB: 219,
    0xDC: 220,
    0xDD: 221,
    0xDE: 222,
    0xDF: 223,
    0xE0: 224,
    0xE1: 225,
    0xE2: 226,
    0xE3: 227,
    0xE4: 228,
    0xE5: 229,
    0xE6: 230,
    0xE7: 231,
    0xE8: 232,
    0xE9: 233,
    0xEA: 234,
    0xEB: 235,
    0xEC: 236,
    0xED: 237,
    0xEE: 238,
    0xEF: 239,
    0xF0: 240,
    0xF1: 241,
    0xF2: 242,
    0xF3: 243,
    0xF4: 244,
    0xF5: 245,
    0xF6: 246,
    0xF7: 247,
    0xF8: 248,
    0xF9: 249,
    0xFA: 250,
    0xFB: 251,
    0xFC: 252,
    0xFD: 253,
    0xFE: 254,
    0xFF: 255,
}
func hex2int(hexB *[]byte) uint64 {
    var retInt uint64
    hexLen := len(*hexB)
    for k, v := range *hexB {
        retInt += b2m_map[v] * exponent(16, uint64(2*(hexLen-k-1)))
    }
    return retInt
}
func exponent(a, n uint64) uint64 {
    result := uint64(1)
    for i := n; i > 0; i >>= 1 {
        if i&1 != 0 {
            result *= a
        }
        a *= a
    }
    return result
}

经过测试,性能和自带的方法接近,略有提升。

?
1
2
3
4
5
6
7
goos: windows
goarch: amd64
pkg: benchmark
Benchmark_General-4     10000000        144 ns/op
Benchmark_Better-4      10000000        128 ns/op
PASS
ok  benchmark   3.086s

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/wangmanjie/article/details/85244958

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部