Skip to content

Commit 56a2b41

Browse files
committed
增加bs类型
1 parent fe877c1 commit 56a2b41

File tree

5 files changed

+137
-124
lines changed

5 files changed

+137
-124
lines changed

conv_any.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ func Array(i interface{}) []interface{} {
151151
return toInterfaces(i)
152152
}
153153

154-
// BIN 任意类型转 []bool 返回长度8的倍数且大于0,true代表二进制的1.
155-
func BIN(i interface{}) []bool {
154+
// BINBool 任意类型转二进制 []bool 返回长度8的倍数且大于0,true代表二进制的1.
155+
func BINBool(i interface{}) []bool {
156156
return toBIN(i)
157157
}
158158

159-
// BINStr 任意类型转 string 长度8的倍数且大于0,由'1'和'0'组成,
159+
// BIN 任意类型转 []bool 返回长度8的倍数且大于0,true代表二进制的1.
160160
// +1 >>> "00000001"
161161
// -1 >>> "11111111"
162-
func BINStr(i interface{}) string {
162+
func BIN(i interface{}) string {
163163
result := ""
164-
for _, v := range BIN(i) {
164+
for _, v := range toBIN(i) {
165165
result += func() string {
166166
if v {
167167
return "1"
@@ -172,27 +172,35 @@ func BINStr(i interface{}) string {
172172
return result
173173
}
174174

175+
// BINStr 任意类型转 string 长度8的倍数且大于0,由'1'和'0'组成,
176+
// +1 >>> "00000001"
177+
// -1 >>> "11111111"
178+
func BINStr(i interface{}) string {
179+
return BIN(i)
180+
}
181+
182+
// OCT 任意类型转8进制 string 长度固定22,8进制,'0'-'7'.
183+
// -1 >>> "1777777777777777777777"
184+
// +1 >>> "0000000000000000000001"
185+
func OCT(i interface{}) string {
186+
return toOCT(i)
187+
}
188+
175189
// OCTStr 任意类型转 string 长度固定22,8进制,'0'-'7'.
176190
// -1 >>> "1777777777777777777777"
177191
// +1 >>> "0000000000000000000001"
178192
func OCTStr(i interface{}) string {
179193
return toOCT(i)
180194
}
181195

182-
// HEXStr 转16进制字符串
183-
func HEXStr(i interface{}) string {
196+
// HEX 转16进制字符串
197+
func HEX(i interface{}) string {
184198
return hex.EncodeToString(Bytes(i))
185199
}
186200

187-
// HEXBytes 16进制的方式转字节
188-
func HEXBytes(i interface{}) []byte {
189-
bs, _ := hex.DecodeString(String(i))
190-
return bs
191-
}
192-
193-
// HEXInt 先16进制转字节,在转Int
194-
func HEXInt(i interface{}) int {
195-
return Int(HEXBytes(i))
201+
// HEXStr 转16进制字符串
202+
func HEXStr(i interface{}) string {
203+
return hex.EncodeToString(Bytes(i))
196204
}
197205

198206
// Copy 复制任意数据

conv_base.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func toBytes(i interface{}) []byte {
5050
}
5151
return toBytes(result)
5252
case *Var:
53-
return toBytes(value.Val())
53+
return value.Bytes()
5454
}
5555
if IsNumber(i) {
5656
// int 类型无法解析
@@ -120,7 +120,7 @@ func toString(i interface{}) string {
120120
bs, _ := ioutil.ReadAll(value)
121121
return string(bs)
122122
case *Var:
123-
return toString(value.Val())
123+
return value.String()
124124
default:
125125
if value == nil {
126126
return ""
@@ -210,7 +210,7 @@ func toInt64(i interface{}) int64 {
210210
case apiInt64:
211211
return value.Int64()
212212
case *Var:
213-
return toInt64(value.Val())
213+
return value.Int64()
214214
default:
215215
s := toString(value)
216216
base := int64(1)
@@ -299,7 +299,7 @@ func toUint64(i interface{}) uint64 {
299299
case apiUint64:
300300
return value.Uint64()
301301
case *Var:
302-
return toUint64(value.Val())
302+
return value.Uint64()
303303
default:
304304
s := toString(value)
305305
// HEX 十六进制
@@ -349,7 +349,7 @@ func toFloat64(i interface{}) float64 {
349349
}
350350
return math.Float64frombits(binary.BigEndian.Uint64(padding(value, 8)))
351351
case *Var:
352-
return toFloat64(value.Val())
352+
return value.Float64()
353353
default:
354354
v, _ := strconv.ParseFloat(toString(i), 64)
355355
return v
@@ -369,7 +369,7 @@ func toBool(i interface{}) bool {
369369
case apiBool:
370370
return value.Bool()
371371
case *Var:
372-
return toBool(value.Val())
372+
return value.Bool()
373373
default:
374374
rv := reflect.ValueOf(i)
375375
switch rv.Kind() {

conv_bytes.go

+79-83
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,44 @@ func (this Bs) Len() int {
2020
return len(this)
2121
}
2222

23+
// Cap cap()
2324
func (this Bs) Cap() int {
2425
return cap(this)
2526
}
2627

28+
// Error 实现error
2729
func (this Bs) Error() string {
2830
return this.String()
2931
}
3032

33+
func (this Bs) Copy() Bs {
34+
cp := Bs(make([]byte, this.Len()))
35+
copy(cp, this)
36+
return cp
37+
}
38+
39+
// Append just append
40+
func (this Bs) Append(b ...byte) Bs {
41+
return append(this, b...)
42+
}
43+
44+
// Upper ASCII小写字母转大小字母
45+
func (this Bs) Upper() Bs {
46+
return bytes.ToUpper(this)
47+
}
48+
49+
// Lower ASCII大写字母转小写字母
50+
func (this Bs) Lower() Bs {
51+
return bytes.ToLower(this)
52+
}
53+
54+
// WriteTo 实现io.WriterTo
3155
func (this Bs) WriteTo(w io.Writer) (int64, error) {
3256
n, err := w.Write(this)
3357
return int64(n), err
3458
}
3559

60+
// Sum 校验和
3661
func (this Bs) Sum() byte {
3762
b := byte(0)
3863
for _, v := range this {
@@ -41,18 +66,9 @@ func (this Bs) Sum() byte {
4166
return b
4267
}
4368

44-
func (this Bs) Copy() Bs {
45-
cp := make([]byte, len(this))
46-
copy(cp, this)
47-
return cp
48-
}
49-
50-
func (this Bs) Upper() Bs {
51-
return bytes.ToUpper(this)
52-
}
53-
54-
func (this Bs) Lower() Bs {
55-
return bytes.ToLower(this)
69+
// Bytes 字节数组
70+
func (this Bs) Bytes() []byte {
71+
return this
5672
}
5773

5874
// String []{0x31,0x32} >>> "12"
@@ -65,9 +81,13 @@ func (this Bs) UTF8() string {
6581
return string(this)
6682
}
6783

68-
// ASCII []{0x31,0x32} >>> "12"
69-
func (this Bs) ASCII() string {
70-
return string(this)
84+
// BIN 字节转2进制字符串
85+
func (this Bs) BIN() string {
86+
return BIN(this)
87+
}
88+
89+
func (this Bs) OCT() string {
90+
return OCT(this)
7191
}
7292

7393
// HEX []{0x01,0x02} >>> "0102"
@@ -82,12 +102,7 @@ func (this Bs) Base64() string {
82102

83103
// HEXBase64 HEX() then Base64()
84104
func (this Bs) HEXBase64() string {
85-
return Bs(this.HEX()).Base64()
86-
}
87-
88-
// Bytes 字节数组
89-
func (this Bs) Bytes() []byte {
90-
return this
105+
return base64.StdEncoding.EncodeToString([]byte(this.HEX()))
91106
}
92107

93108
// Reader io.Reader
@@ -167,101 +182,82 @@ func (this Bs) Uint64() uint64 {
167182
return Uint64(this.Bytes())
168183
}
169184

170-
// BINStr 字节转2进制字符串
171-
func (this Bs) BINStr() string {
172-
return BINStr(this)
185+
func (this Bs) Float32() float32 {
186+
return Float32(this.Bytes())
173187
}
174188

175-
// BIN 字节转2进制字符串
176-
func (this Bs) BIN() string {
177-
return BINStr(this)
189+
func (this Bs) Float64() float64 {
190+
return Float64(this.Bytes())
178191
}
179192

180-
// Append just append
181-
func (this Bs) Append(b ...byte) Bs {
182-
return append(this, b...)
193+
func (this Bs) Float() float64 {
194+
return Float64(this.Bytes())
183195
}
184196

185-
// UTF8ToInt []{0x31,0x32} >>> 12
186-
func (this Bs) UTF8ToInt() (int, error) {
187-
return strconv.Atoi(this.ASCII())
197+
func (this Bs) Bool() bool {
198+
return Bool(this.Bytes())
188199
}
189200

190-
// UTF8ToFloat64 字节utf8编码再转int,再转float64
191-
func (this Bs) UTF8ToFloat64(decimals int) (float64, error) {
192-
i, err := this.UTF8ToInt()
193-
return float64(i) / math.Pow10(decimals), err
194-
}
195-
196-
// HEXToInt []{0x01,0x02} >>> 102
197-
func (this Bs) HEXToInt() (int, error) {
198-
return strconv.Atoi(this.HEX())
201+
func (this Bs) Float64frombits() float64 {
202+
return math.Float64frombits(this.Uint64())
199203
}
200204

201-
// HEXToFloat64 字节hex编码再转int,再转float64
202-
func (this Bs) HEXToFloat64(decimals int) (float64, error) {
203-
i, err := this.HEXToInt()
204-
return float64(i) / math.Pow10(decimals), err
205+
func (this Bs) Float32frombits() float32 {
206+
return math.Float32frombits(this.Uint32())
205207
}
206208

207209
// Reverse 倒序
208210
func (this Bs) Reverse() Bs {
209-
x := make([]byte, len(this))
210-
for i, v := range this {
211-
x[len(this)-i-1] = v
211+
for i := 0; i < this.Len()/2; i++ {
212+
this[i], this[this.Len()-i-1] = this[this.Len()-i-1], this[i]
212213
}
213-
return x
214-
}
215-
216-
// ReverseASCII 倒序再ASCII
217-
func (this Bs) ReverseASCII() string {
218-
return this.Reverse().ASCII()
214+
return this
219215
}
220216

221-
// ReverseHEX 倒序再hex
222-
func (this Bs) ReverseHEX() string {
223-
return this.Reverse().HEX()
217+
// Add 每个字节加add
218+
func (this Bs) Add(add byte) Bs {
219+
for i, v := range this {
220+
this[i] = v + add
221+
}
222+
return this
224223
}
225224

226-
// ReverseBase64 倒序再base64
227-
func (this Bs) ReverseBase64() string {
228-
return this.Reverse().Base64()
225+
// Sub 每个字节减sub
226+
func (this Bs) Sub(sub byte) Bs {
227+
for i, v := range this {
228+
this[i] = v - sub
229+
}
230+
return this
229231
}
230232

231-
// AddByte 每个字节加add
232-
func (this Bs) AddByte(add byte) Bs {
233-
result := make([]byte, len(this))
234-
for _, v := range this {
235-
result = append(result, v+add)
236-
}
237-
return result
233+
// UTF8ToInt []{0x31,0x32} >>> 12
234+
func (this Bs) UTF8ToInt() (int, error) {
235+
return strconv.Atoi(this.UTF8())
238236
}
239237

240-
// SubByte 每个字节减sub
241-
func (this Bs) SubByte(sub byte) Bs {
242-
result := make([]byte, len(this))
243-
for _, v := range this {
244-
result = append(result, v-sub)
245-
}
246-
return result
238+
// UTF8ToFloat64 字节utf8编码再转int,再转float64
239+
func (this Bs) UTF8ToFloat64(decimals int) (float64, error) {
240+
i, err := this.UTF8ToInt()
241+
return float64(i) / math.Pow10(decimals), err
247242
}
248243

249-
// Sub0x33 每个字节减0x33
250-
func (this Bs) Sub0x33() Bs {
251-
return this.SubByte(0x33)
244+
// HEXToInt []{0x01,0x02} >>> 102
245+
func (this Bs) HEXToInt() (int, error) {
246+
return strconv.Atoi(this.HEX())
252247
}
253248

254-
// Add0x33 每个字节加0x33
255-
func (this Bs) Add0x33() Bs {
256-
return this.AddByte(0x33)
249+
// HEXToFloat64 字节hex编码再转int,再转float64
250+
func (this Bs) HEXToFloat64(decimals int) (float64, error) {
251+
i, err := this.HEXToInt()
252+
return float64(i) / math.Pow10(decimals), err
257253
}
258254

259255
// Sub0x33ReverseHEXToInt DLT645协议流程,先减0x33,再倒序,再转hex,再转int
260256
func (this Bs) Sub0x33ReverseHEXToInt() (int, error) {
261-
return this.Sub0x33().Reverse().HEXToInt()
257+
return this.Sub(0x33).Reverse().HEXToInt()
262258
}
263259

264260
// Sub0x33ReverseHEXToFloat DLT645协议流程,先减0x33,再倒序,再转hex,再转float64
265261
func (this Bs) Sub0x33ReverseHEXToFloat(decimals int) (float64, error) {
266-
return this.Sub0x33().Reverse().HEXToFloat64(decimals)
262+
return this.Sub(0x33).Reverse().HEXToFloat64(decimals)
267263
}

0 commit comments

Comments
 (0)