-
Notifications
You must be signed in to change notification settings - Fork 1
/
scale.go
522 lines (485 loc) · 12.6 KB
/
scale.go
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright (c) 2019. aberic - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package gnomon
import (
"math"
"strconv"
"strings"
)
var (
hexIntMap = map[int]string{ // hexIntMap 十六进制对应十进制映射
0: "0", 1: "1", 2: "2", 3: "3",
4: "4", 5: "5", 6: "6", 7: "7",
8: "8", 9: "9", 10: "a", 11: "b",
12: "c", 13: "d", 14: "e", 15: "f",
}
intHexMap = map[string]int{ // intHexMap 十进制对应十六进制映射
"0": 0, "1": 1, "2": 2, "3": 3,
"4": 4, "5": 5, "6": 6, "7": 7,
"8": 8, "9": 9, "a": 10, "b": 11,
"c": 12, "d": 13, "e": 14, "f": 15,
}
duoIntMap = map[int]string{ // duoIntMap 三十二进制对应十进制映射
0: "0", 1: "1", 2: "2", 3: "3",
4: "4", 5: "5", 6: "6", 7: "7",
8: "8", 9: "9", 10: "a", 11: "b",
12: "c", 13: "d", 14: "e", 15: "f",
16: "g", 17: "h", 18: "i", 19: "j",
20: "k", 21: "l", 22: "m", 23: "n",
24: "o", 25: "p", 26: "q", 27: "r",
28: "s", 29: "t", 30: "u", 31: "v",
}
intDuoMap = map[string]int{ // intDuoMap 十进制对应三十二进制映射
"0": 0, "1": 1, "2": 2, "3": 3,
"4": 4, "5": 5, "6": 6, "7": 7,
"8": 8, "9": 9, "a": 10, "b": 11,
"c": 12, "d": 13, "e": 14, "f": 15,
"g": 16, "h": 17, "i": 18, "j": 19,
"k": 20, "l": 21, "m": 22, "n": 23,
"o": 24, "p": 25, "q": 26, "r": 27,
"s": 28, "t": 29, "u": 30, "v": 31,
}
dDuoIntMap = map[int]string{ // dDuoIntMap 64进制对应十进制映射
0: "0", 1: "1", 2: "2", 3: "3",
4: "4", 5: "5", 6: "6", 7: "7",
8: "8", 9: "9", 10: "a", 11: "b",
12: "c", 13: "d", 14: "e", 15: "f",
16: "g", 17: "h", 18: "i", 19: "j",
20: "k", 21: "l", 22: "m", 23: "n",
24: "o", 25: "p", 26: "q", 27: "r",
28: "s", 29: "t", 30: "u", 31: "v",
32: "w", 33: "x", 34: "y", 35: "z",
36: "A", 37: "B", 38: "C", 39: "D",
40: "E", 41: "F", 42: "G", 43: "H",
44: "I", 45: "J", 46: "K", 47: "L",
48: "M", 49: "N", 50: "O", 51: "P",
52: "Q", 53: "R", 54: "S", 55: "T",
56: "U", 57: "V", 58: "W", 59: "X",
60: "Y", 61: "Z", 62: "+", 63: "-",
}
intDDuoMap = map[string]int{ // intDDuoMap 十进制对应64进制映射
"0": 0, "1": 1, "2": 2, "3": 3,
"4": 4, "5": 5, "6": 6, "7": 7,
"8": 8, "9": 9, "a": 10, "b": 11,
"c": 12, "d": 13, "e": 14, "f": 15,
"g": 16, "h": 17, "i": 18, "j": 19,
"k": 20, "l": 21, "m": 22, "n": 23,
"o": 24, "p": 25, "q": 26, "r": 27,
"s": 28, "t": 29, "u": 30, "v": 31,
"w": 32, "x": 33, "y": 34, "z": 35,
"A": 36, "B": 37, "C": 38, "D": 39,
"E": 40, "F": 41, "G": 42, "H": 43,
"I": 44, "J": 45, "K": 46, "L": 47,
"M": 48, "N": 49, "O": 50, "P": 51,
"Q": 52, "R": 53, "S": 54, "T": 55,
"U": 56, "V": 57, "W": 58, "X": 59,
"Y": 60, "Z": 61, "+": 62, "-": 63,
}
)
// ScaleUint64ToHexString uint64转十六进制字符串
func ScaleUint64ToHexString(i uint64) string {
iSt := ""
for i > 0 {
if i >= 16 {
iSt = strings.Join([]string{hexIntMap[int(i%16)], iSt}, "")
i /= 16
} else if i > 0 && i < 16 {
iSt = strings.Join([]string{hexIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleInt64ToHexString int64转十六进制字符串
func ScaleInt64ToHexString(i int64) string {
iSt := ""
for i > 0 {
if i >= 16 {
iSt = strings.Join([]string{hexIntMap[int(i%16)], iSt}, "")
i /= 16
} else if i > 0 && i < 16 {
iSt = strings.Join([]string{hexIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleUint32ToHexString uint32转十六进制字符串
func ScaleUint32ToHexString(i uint32) string {
iSt := ""
for i > 0 {
if i >= 16 {
iSt = strings.Join([]string{hexIntMap[int(i%16)], iSt}, "")
i /= 16
} else if i > 0 && i < 16 {
iSt = strings.Join([]string{hexIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleInt32ToHexString int32转十六进制字符串
func ScaleInt32ToHexString(i int32) string {
iSt := ""
for i > 0 {
if i >= 16 {
iSt = strings.Join([]string{hexIntMap[int(i%16)], iSt}, "")
i /= 16
} else if i > 0 && i < 16 {
iSt = strings.Join([]string{hexIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleUintToHexString uint转十六进制字符串
func ScaleUintToHexString(i uint) string {
iSt := ""
for i > 0 {
if i >= 16 {
iSt = strings.Join([]string{hexIntMap[int(i%16)], iSt}, "")
i /= 16
} else if i > 0 && i < 16 {
iSt = strings.Join([]string{hexIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleIntToHexString int转十六进制字符串
func ScaleIntToHexString(i int) string {
iSt := ""
for i > 0 {
if i >= 16 {
iSt = strings.Join([]string{hexIntMap[int(i%16)], iSt}, "")
i /= 16
} else if i > 0 && i < 16 {
iSt = strings.Join([]string{hexIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleHexStringToUint64 int字符串转int
func ScaleHexStringToUint64(hex string) uint64 {
hexLen := len(hex)
var uint64Hex uint64
for i := 0; i < hexLen; i++ {
uint64Hex += uint64(intHexMap[hex[i:i+1]]) * uint64(math.Pow(16, float64(hexLen-i-1)))
}
return uint64Hex
}
// ScaleHexStringToInt64 int字符串转int
func ScaleHexStringToInt64(hex string) int64 {
hexLen := len(hex)
var int64Hex int64
for i := 0; i < hexLen; i++ {
int64Hex += int64(intHexMap[hex[i:i+1]]) * int64(math.Pow(16, float64(hexLen-i-1)))
}
return int64Hex
}
// ScaleUint64ToDuoString uint64转十六进制字符串
func ScaleUint64ToDuoString(i uint64) string {
iSt := ""
for i > 0 {
if i >= 32 {
iSt = strings.Join([]string{duoIntMap[int(i%32)], iSt}, "")
i /= 32
} else if i > 0 && i < 32 {
iSt = strings.Join([]string{duoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleInt64ToDuoString int64转十六进制字符串
func ScaleInt64ToDuoString(i int64) string {
iSt := ""
for i > 0 {
if i >= 32 {
iSt = strings.Join([]string{duoIntMap[int(i%32)], iSt}, "")
i /= 32
} else if i > 0 && i < 32 {
iSt = strings.Join([]string{duoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleUint32ToDuoString uint32转十六进制字符串
func ScaleUint32ToDuoString(i uint32) string {
iSt := ""
for i > 0 {
if i >= 32 {
iSt = strings.Join([]string{duoIntMap[int(i%32)], iSt}, "")
i /= 32
} else if i > 0 && i < 32 {
iSt = strings.Join([]string{duoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleInt32ToDuoString int32转十六进制字符串
func ScaleInt32ToDuoString(i int32) string {
iSt := ""
for i > 0 {
if i >= 32 {
iSt = strings.Join([]string{duoIntMap[int(i%32)], iSt}, "")
i /= 32
} else if i > 0 && i < 32 {
iSt = strings.Join([]string{duoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleUintToDuoString uint转十六进制字符串
func ScaleUintToDuoString(i uint) string {
iSt := ""
for i > 0 {
if i >= 32 {
iSt = strings.Join([]string{duoIntMap[int(i%32)], iSt}, "")
i /= 32
} else if i > 0 && i < 32 {
iSt = strings.Join([]string{duoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleIntToDuoString int转十六进制字符串
func ScaleIntToDuoString(i int) string {
iSt := ""
for i > 0 {
if i >= 32 {
iSt = strings.Join([]string{duoIntMap[int(i%32)], iSt}, "")
i /= 32
} else if i > 0 && i < 32 {
iSt = strings.Join([]string{duoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleDuoStringToUint64 int字符串转int
func ScaleDuoStringToUint64(duo string) uint64 {
duoLen := len(duo)
var uint64Duo uint64
for i := 0; i < duoLen; i++ {
uint64Duo += uint64(intDuoMap[duo[i:i+1]]) * uint64(math.Pow(32, float64(duoLen-i-1)))
}
return uint64Duo
}
// ScaleDuoStringToInt64 int字符串转int
func ScaleDuoStringToInt64(duo string) int64 {
duoLen := len(duo)
var int64Duo int64
for i := 0; i < duoLen; i++ {
int64Duo += int64(intDuoMap[duo[i:i+1]]) * int64(math.Pow(32, float64(duoLen-i-1)))
}
return int64Duo
}
// ScaleUint64ToDDuoString uint64转十六进制字符串
func ScaleUint64ToDDuoString(i uint64) string {
iSt := ""
for i > 0 {
if i >= 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i%64)], iSt}, "")
i /= 64
} else if i > 0 && i < 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleInt64ToDDuoString int64转十六进制字符串
func ScaleInt64ToDDuoString(i int64) string {
iSt := ""
for i > 0 {
if i >= 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i%64)], iSt}, "")
i /= 64
} else if i > 0 && i < 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleUint32ToDDuoString uint32转十六进制字符串
func ScaleUint32ToDDuoString(i uint32) string {
iSt := ""
for i > 0 {
if i >= 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i%64)], iSt}, "")
i /= 64
} else if i > 0 && i < 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleInt32ToDDuoString int32转十六进制字符串
func ScaleInt32ToDDuoString(i int32) string {
iSt := ""
for i > 0 {
if i >= 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i%64)], iSt}, "")
i /= 64
} else if i > 0 && i < 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleUintToDDuoString int转64进制字符串
func ScaleUintToDDuoString(i uint) string {
iSt := ""
for i > 0 {
if i >= 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i%64)], iSt}, "")
i /= 64
} else if i > 0 && i < 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleIntToDDuoString int转64进制字符串
func ScaleIntToDDuoString(i int) string {
iSt := ""
for i > 0 {
if i >= 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i%64)], iSt}, "")
i /= 64
} else if i > 0 && i < 64 {
iSt = strings.Join([]string{dDuoIntMap[int(i)], iSt}, "")
i = 0
}
}
return iSt
}
// ScaleDDuoStringToUint64 int字符串转int
func ScaleDDuoStringToUint64(dDuo string) uint64 {
dDuoLen := len(dDuo)
var uint64DDuo uint64
for i := 0; i < dDuoLen; i++ {
uint64DDuo += uint64(intDDuoMap[dDuo[i:i+1]]) * uint64(math.Pow(64, float64(dDuoLen-i-1)))
}
return uint64DDuo
}
// ScaleDDuoStringToInt64 int字符串转int
func ScaleDDuoStringToInt64(dDuo string) int64 {
dDuoLen := len(dDuo)
var int64DDuo int64
for i := 0; i < dDuoLen; i++ {
int64DDuo += int64(intDDuoMap[dDuo[i:i+1]]) * int64(math.Pow(64, float64(dDuoLen-i-1)))
}
return int64DDuo
}
// ScaleUint64Len 计算整型字符串长度
func ScaleUint64Len(i uint64) int {
iLen := 1
for i >= 10 {
i /= 10
iLen++
}
return iLen
}
// ScaleInt64Len 计算整型字符串长度
func ScaleInt64Len(i int64) int {
iLen := 1
for i >= 10 {
i /= 10
iLen++
}
return iLen
}
// ScaleUint32Len 计算整型字符串长度
func ScaleUint32Len(i uint32) int {
iLen := 1
for i >= 10 {
i /= 10
iLen++
}
return iLen
}
// ScaleInt32Len 计算整型字符串长度
func ScaleInt32Len(i int32) int {
iLen := 1
for i >= 10 {
i /= 10
iLen++
}
return iLen
}
// ScaleUintLen 计算整型字符串长度
func ScaleUintLen(i uint) int {
iLen := 1
for i >= 10 {
i /= 10
iLen++
}
return iLen
}
// ScaleIntLen 计算整型字符串长度
func ScaleIntLen(i int) int {
iLen := 1
for i >= 10 {
i /= 10
iLen++
}
return iLen
}
// ScaleUint8toFullState 补全不满三位数状态,如1->001、34->034、215->215
func ScaleUint8toFullState(index uint8) string {
result := strconv.Itoa(int(index))
if index < 10 {
return strings.Join([]string{"00", result}, "")
} else if index < 100 {
return strings.Join([]string{"0", result}, "")
}
return result
}
// ScaleUint32toFullState 补全不满十位数状态,如1->0000000001、34->0000000034、215->0000000215
func ScaleUint32toFullState(index uint32) string {
indexStr := strconv.Itoa(int(index))
if ScaleUint32Len(index) >= 10 {
return indexStr
}
pos := 0
for index > 1 {
index /= 10
pos++
}
backZero := 10 - pos
for i := 0; i < backZero; i++ {
indexStr = strings.Join([]string{"0", indexStr}, "")
}
return indexStr
}
// ScaleFloat64toInt64 将float64转成精确的int64
func ScaleFloat64toInt64(num float64, retain int) int64 {
return int64(num * math.Pow10(retain))
}
// ScaleInt64toFloat64 将int64恢复成正常的float64
func ScaleInt64toFloat64(num int64, retain int) float64 {
return float64(num) / math.Pow10(retain)
}