-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.go
More file actions
96 lines (88 loc) · 2.23 KB
/
Copy pathcodec.go
File metadata and controls
96 lines (88 loc) · 2.23 KB
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
package bcd8421
import (
"bytes"
"fmt"
"strconv"
)
// bytesLength 是生成的 BCD 长度,如果 number 长度不足,在前面补 0
func EncodeFromStr(number string, bytesLength int) ([]byte, error) {
var (
numberBytes []byte
numberLength = len(number)
)
if bytesLength*2 < numberLength {
return numberBytes, fmt.Errorf("invalid bytesLength")
}
nb, err := stringNumberToBytes(number)
if err != nil {
return numberBytes, err
}
if numberLength%2 == 1 {
nb = append([]byte{0x00}, nb...)
}
if fill := bytesLength*2 - len(nb); fill != 0 {
nb = append(bytes.Repeat([]byte{0x00}, fill), nb...)
}
// n1 左移 4 位,高位丢弃,低位补 0,得到 n3
// n3 n2 进行或操作,即可将 n1 的低 4 位和 n2 的低 4 位压缩在一起
for i := 0; i < len(nb); i += 2 {
n1 := nb[i]
n2 := nb[i+1]
n3 := n1 << 4
numberBytes = append(numberBytes, n3|n2)
// fmt.Printf("%d, %d : %#b %#b %#b %#b\n", nb[i], nb[i+1], n1, n2, n3, n3|n2)
}
return numberBytes, nil
}
// skipzero 是否跳过数字前面的 0
func DecodeToStr(src []byte, skipzero bool) (string, error) {
var (
s string
foundFirst bool
)
for _, b := range src {
if b == 0x00 && !foundFirst && skipzero {
continue
}
// 高 4 位直接是第一个数字
n1 := b >> 4
// 将低 4 位左移到高 4 位,在将高低 4 位调换位置得到 n2
mask := b << 4
n2 := mask<<4 | mask>>4
// fmt.Printf("%#b n1:%#b %#b n2:%#b\n", b, n1, b<<4, n2)
if n1 > 9 || n2 > 9 {
return s, fmt.Errorf("invalid BCD 8421 bytes")
}
if !skipzero {
s += strconv.Itoa(int(n1))
s += strconv.Itoa(int(n2))
continue
}
if n1 != 0x00 && !foundFirst {
foundFirst = true
}
if n1 != 0x00 || foundFirst {
s += strconv.Itoa(int(n1))
}
if n2 != 0x00 && !foundFirst {
foundFirst = true
}
if n2 != 0x00 || foundFirst {
s += strconv.Itoa(int(n2))
}
}
return s, nil
}
// 将字符串数字逐个先转为数字,再转为 byte
func stringNumberToBytes(number string) ([]byte, error) {
const fnAtoi = "stringNumberToBytes"
var b []byte
for _, ch := range []byte(number) {
ch -= '0'
if ch > 9 {
return b, &strconv.NumError{Func: fnAtoi, Num: number, Err: strconv.ErrSyntax}
}
b = append(b, ch)
}
return b, nil
}