forked from ideazxy/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcd.go
44 lines (37 loc) · 816 Bytes
/
bcd.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
package iso8583
import (
"encoding/hex"
)
func lbcd(data []byte) []byte {
if len(data)%2 != 0 {
return bcd(append(data, "0"...))
}
return bcd(data)
}
func rbcd(data []byte) []byte {
if len(data)%2 != 0 {
return bcd(append([]byte("0"), data...))
}
return bcd(data)
}
// Encode numeric in ascii into bsd (be sure len(data) % 2 == 0)
func bcd(data []byte) []byte {
out := make([]byte, len(data)/2+1)
n, err := hex.Decode(out, data)
if err != nil {
panic(err.Error())
}
return out[:n]
}
func bcdl2Ascii(data []byte, length int) []byte {
return bcd2Ascii(data)[:length]
}
func bcdr2Ascii(data []byte, length int) []byte {
out := bcd2Ascii(data)
return out[len(out)-length:]
}
func bcd2Ascii(data []byte) []byte {
out := make([]byte, len(data)*2)
n := hex.Encode(out, data)
return out[:n]
}