@@ -20,19 +20,44 @@ func (this Bs) Len() int {
20
20
return len (this )
21
21
}
22
22
23
+ // Cap cap()
23
24
func (this Bs ) Cap () int {
24
25
return cap (this )
25
26
}
26
27
28
+ // Error 实现error
27
29
func (this Bs ) Error () string {
28
30
return this .String ()
29
31
}
30
32
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
31
55
func (this Bs ) WriteTo (w io.Writer ) (int64 , error ) {
32
56
n , err := w .Write (this )
33
57
return int64 (n ), err
34
58
}
35
59
60
+ // Sum 校验和
36
61
func (this Bs ) Sum () byte {
37
62
b := byte (0 )
38
63
for _ , v := range this {
@@ -41,18 +66,9 @@ func (this Bs) Sum() byte {
41
66
return b
42
67
}
43
68
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
56
72
}
57
73
58
74
// String []{0x31,0x32} >>> "12"
@@ -65,9 +81,13 @@ func (this Bs) UTF8() string {
65
81
return string (this )
66
82
}
67
83
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 )
71
91
}
72
92
73
93
// HEX []{0x01,0x02} >>> "0102"
@@ -82,12 +102,7 @@ func (this Bs) Base64() string {
82
102
83
103
// HEXBase64 HEX() then Base64()
84
104
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 ()))
91
106
}
92
107
93
108
// Reader io.Reader
@@ -167,101 +182,82 @@ func (this Bs) Uint64() uint64 {
167
182
return Uint64 (this .Bytes ())
168
183
}
169
184
170
- // BINStr 字节转2进制字符串
171
- func (this Bs ) BINStr () string {
172
- return BINStr (this )
185
+ func (this Bs ) Float32 () float32 {
186
+ return Float32 (this .Bytes ())
173
187
}
174
188
175
- // BIN 字节转2进制字符串
176
- func (this Bs ) BIN () string {
177
- return BINStr (this )
189
+ func (this Bs ) Float64 () float64 {
190
+ return Float64 (this .Bytes ())
178
191
}
179
192
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 ())
183
195
}
184
196
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 ())
188
199
}
189
200
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 ())
199
203
}
200
204
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 ())
205
207
}
206
208
207
209
// Reverse 倒序
208
210
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 ]
212
213
}
213
- return x
214
- }
215
-
216
- // ReverseASCII 倒序再ASCII
217
- func (this Bs ) ReverseASCII () string {
218
- return this .Reverse ().ASCII ()
214
+ return this
219
215
}
220
216
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
224
223
}
225
224
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
229
231
}
230
232
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 ())
238
236
}
239
237
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
247
242
}
248
243
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 () )
252
247
}
253
248
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
257
253
}
258
254
259
255
// Sub0x33ReverseHEXToInt DLT645协议流程,先减0x33,再倒序,再转hex,再转int
260
256
func (this Bs ) Sub0x33ReverseHEXToInt () (int , error ) {
261
- return this .Sub0x33 ( ).Reverse ().HEXToInt ()
257
+ return this .Sub ( 0x33 ).Reverse ().HEXToInt ()
262
258
}
263
259
264
260
// Sub0x33ReverseHEXToFloat DLT645协议流程,先减0x33,再倒序,再转hex,再转float64
265
261
func (this Bs ) Sub0x33ReverseHEXToFloat (decimals int ) (float64 , error ) {
266
- return this .Sub0x33 ( ).Reverse ().HEXToFloat64 (decimals )
262
+ return this .Sub ( 0x33 ).Reverse ().HEXToFloat64 (decimals )
267
263
}
0 commit comments