/
convert.go
193 lines (169 loc) · 3.76 KB
/
convert.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
package utils
import (
"bytes"
"fmt"
"hash/fnv"
"strconv"
)
// 转换字符串以指定类型。
type StrTo string
func (f StrTo) Exist() bool {
return string(f) != string(rune(0x1E))
}
func (f StrTo) Uint8() (uint8, error) {
v, err := strconv.ParseUint(f.String(), 10, 8)
return uint8(v), err
}
func (f StrTo) Int() (int, error) {
v, err := strconv.ParseInt(f.String(), 10, 0)
return int(v), err
}
func (f StrTo) Int64() (int64, error) {
v, err := strconv.ParseInt(f.String(), 10, 64)
return int64(v), err
}
func (f StrTo) MustUint8() uint8 {
v, _ := f.Uint8()
return v
}
func (f StrTo) MustInt() int {
v, _ := f.Int()
return v
}
func (f StrTo) MustInt64() int64 {
v, _ := f.Int64()
return v
}
func (f StrTo) Float64() float64 {
v, _ := strconv.ParseFloat(f.String(), 10)
return v
}
func (f StrTo) String() string {
if f.Exist() {
return string(f)
}
return ""
}
func (f StrTo) Hash() string {
if len(f) > 0 {
h := fnv.New32a()
h.Write([]byte(f))
return strconv.FormatUint(uint64(h.Sum32()), 10)
} else {
return ""
}
}
// 将任何类型转换为字符串。
func ToStr(value interface{}, args ...int) (s string) {
switch v := value.(type) {
case bool:
s = strconv.FormatBool(v)
case float32:
s = strconv.FormatFloat(float64(v), 'f', ArgInt(args).Get(0, -1), ArgInt(args).Get(1, 32))
case float64:
s = strconv.FormatFloat(v, 'f', ArgInt(args).Get(0, -1), ArgInt(args).Get(1, 64))
case int:
s = strconv.FormatInt(int64(v), ArgInt(args).Get(0, 10))
case int8:
s = strconv.FormatInt(int64(v), ArgInt(args).Get(0, 10))
case int16:
s = strconv.FormatInt(int64(v), ArgInt(args).Get(0, 10))
case int32:
s = strconv.FormatInt(int64(v), ArgInt(args).Get(0, 10))
case int64:
s = strconv.FormatInt(v, ArgInt(args).Get(0, 10))
case uint:
s = strconv.FormatUint(uint64(v), ArgInt(args).Get(0, 10))
case uint8:
s = strconv.FormatUint(uint64(v), ArgInt(args).Get(0, 10))
case uint16:
s = strconv.FormatUint(uint64(v), ArgInt(args).Get(0, 10))
case uint32:
s = strconv.FormatUint(uint64(v), ArgInt(args).Get(0, 10))
case uint64:
s = strconv.FormatUint(v, ArgInt(args).Get(0, 10))
case string:
s = v
case []byte:
s = string(v)
default:
s = fmt.Sprintf("%v", v)
}
return s
}
type ArgInt []int
func (a ArgInt) Get(i int, args ...int) (r int) {
if i >= 0 && i < len(a) {
r = a[i]
} else if len(args) > 0 {
r = args[0]
}
return
}
type ArgString []string
func (a ArgString) Get(i int, args ...string) (r string) {
if i >= 0 && i < len(a) {
r = a[i]
} else if len(args) > 0 {
r = args[0]
}
return
}
type ArgAny []interface{}
func (a ArgAny) Get(i int, args ...interface{}) (r interface{}) {
if i >= 0 && i < len(a) {
r = a[i]
}
if len(args) > 0 {
r = args[0]
}
return
}
// 将十六进制格式字符串转换为十进制数.
func HexStr2int(hexStr string) (int, error) {
num := 0
length := len(hexStr)
for i := 0; i < length; i++ {
char := hexStr[length-i-1]
factor := -1
switch {
case char >= '0' && char <= '9':
factor = int(char) - '0'
case char >= 'a' && char <= 'f':
factor = int(char) - 'a' + 10
default:
return -1, fmt.Errorf("invalid hex: %s", string(char))
}
num += factor * PowInt(16, i)
}
return num, nil
}
// 将十进制数格式转换为十六进制字符串.
func Int2HexStr(num int) (hex string) {
if num == 0 {
return "0"
}
for num > 0 {
r := num % 16
c := "?"
if r >= 0 && r <= 9 {
c = string(rune(r + '0'))
} else {
c = string(rune(r + 'a' - 10))
}
hex = c + hex
num = num / 16
}
return hex
}
// 整形数组转换成字符串
func ArrayToString(A []int, denim string) string {
var buffer bytes.Buffer
for i := 0; i < len(A); i++ {
buffer.WriteString(strconv.Itoa(A[i]))
if i != len(A)-1 {
buffer.WriteString(denim)
}
}
return buffer.String()
}