-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_msi.go
74 lines (67 loc) · 1.37 KB
/
code_msi.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
package barcode
import (
"fmt"
"strconv"
)
var msiChars = map[string]string{
"0": "100100100100",
"1": "100100100110",
"2": "100100110100",
"3": "100100110110",
"4": "100110100100",
"5": "100110100110",
"6": "100110110100",
"7": "100110110110",
"8": "110100100100",
"9": "110100100110",
"A": "110100110100",
"B": "110100110110",
"C": "110110100100",
"D": "110110100110",
"E": "110110110100",
"F": "110110110110",
}
/** barcodeMSI
* Variation of Plessey code, with similiar applications
* Contains digits (0-9) and encodes the data only in the width of bars
*/
func barcodeMSI(code string, checksum bool) *barArray {
if !onlyDigits(code) {
fmt.Printf("[BARCODE] code may only contain digits but got %s\n", code)
return nil
}
if checksum {
// add checksum
p := 2
check := 0
for i := (len(code) - 1); i >= 0; i-- {
intVal, _ := strconv.Atoi(string(code[i]))
check = check + (intVal * p)
p++
if p > 7 {
p = 2
}
}
check %= 11
if check > 0 {
check = 11 - check
}
code = code + strconv.Itoa(check)
}
seq := "110" // left guard
for i := 0; i < len(code); i++ {
digit := string(code[i])
if _, ok := msiChars[digit]; !ok {
return nil
}
seq = seq + msiChars[digit]
}
seq = seq + "1001" // right guard
bararray := &barArray{
Code: code,
MaxW: 0,
MaxH: 1,
BCode: []bCode{},
}
return binaryToArr(seq, bararray)
}