Skip to content

Instantly share code, notes, and snippets.

@doggy8088
Last active December 1, 2021 15:20
Show Gist options
  • Save doggy8088/b56bce39410612c7c9964baa65dd50f1 to your computer and use it in GitHub Desktop.
Save doggy8088/b56bce39410612c7c9964baa65dd50f1 to your computer and use it in GitHub Desktop.
Value Set A Set B Set C ASCII Character Pattern
0 (Space) (Space) 0 32 / 207 (Space) / Ï 11011001100
1 ! ! 1 33 ! 11001101100
2 " " 2 34 " 11001100110
3 # # 3 35 # 10010011000
4 $ $ 4 36 $ 10010001100
5 % % 5 37 % 10001001100
6 & & 6 38 & 10011001000
7 ' ' 7 39 ' 10011000100
8 ( ( 8 40 ( 10001100100
9 ) ) 9 41 ) 11001001000
10 * * 10 42 * 11001000100
11 + + 11 43 + 11000100100
12 , , 12 44 , 10110011100
13 - - 13 45 - 10011011100
14 . . 14 46 . 10011001110
15 / / 15 47 / 10111001100
16 0 0 16 48 0 10011101100
17 1 1 17 49 1 10011100110
18 2 2 18 50 2 11001110010
19 3 3 19 51 3 11001011100
20 4 4 20 52 4 11001001110
21 5 5 21 53 5 11011100100
22 6 6 22 54 6 11001110100
23 7 7 23 55 7 11101101110
24 8 8 24 56 8 11101001100
25 9 9 25 57 9 11100101100
26 : : 26 58 : 11100100110
27 ; ; 27 59 ; 11101100100
28 < < 28 60 < 11100110100
29 = = 29 61 = 11100110010
30 > > 30 62 > 11011011000
31 ? ? 31 63 ? 11011000110
32 @ @ 32 64 @ 11000110110
33 A A 33 65 A 10100011000
34 B B 34 66 B 10001011000
35 C C 35 67 C 10001000110
36 D D 36 68 D 10110001000
37 E E 37 69 E 10001101000
38 F F 38 70 F 10001100010
39 G G 39 71 G 11010001000
40 H H 40 72 H 11000101000
41 I I 41 73 I 11000100010
42 J J 42 74 J 10110111000
43 K K 43 75 K 10110001110
44 L L 44 76 L 10001101110
45 M M 45 77 M 10111011000
46 N N 46 78 N 10111000110
47 O O 47 79 O 10001110110
48 P P 48 80 P 11101110110
49 Q Q 49 81 Q 11010001110
50 R R 50 82 R 11000101110
51 S S 51 83 S 11011101000
52 T T 52 84 T 11011100010
53 U U 53 85 U 11011101110
54 V V 54 86 V 11101011000
55 W W 55 87 W 11101000110
56 X X 56 88 X 11100010110
57 Y Y 57 89 Y 11101101000
58 Z Z 58 90 Z 11101100010
59 [ [ 59 91 [ 11100011010
60 \ \ 60 92 \ 11101111010
61 ] ] 61 93 ] 11001000010
62 ^ ^ 62 94 ^ 11110001010
63 _ _ 63 95 _ 10100110000
64 nul ` 64 96 ` 10100001100
65 soh a 65 97 a 10010110000
66 stx b 66 98 b 10010000110
67 etx c 67 99 c 10000101100
68 eot d 68 100 d 10000100110
69 eno e 69 101 e 10110010000
70 ack f 70 102 f 10110000100
71 bel g 71 103 g 10011010000
72 bs h 72 104 h 10011000010
73 ht i 73 105 i 10000110100
74 lf j 74 106 j 10000110010
75 vt k 75 107 k 11000010010
76 ff l 76 108 l 11001010000
77 cr m 77 109 m 11110111010
78 s0 n 78 110 n 11000010100
79 s1 o 79 111 o 10001111010
80 dle p 80 112 p 10100111100
81 dc1 q 81 113 q 10010111100
82 dc2 r 82 114 r 10010011110
83 dc3 s 83 115 s 10111100100
84 dc4 t 84 116 t 10011110100
85 nak u 85 117 u 10011110010
86 syn v 86 118 v 11110100100
87 etb w 87 119 w 11110010100
88 can x 88 120 x 11110010010
89 em y 89 121 y 11011011110
90 sub z 90 122 z 11011110110
91 esc { 91 123 { 11110110110
92 fs | 92 124 | 10101111000
93 gs } 93 125 } 10100011110
94 rs ~ 94 126 ~ 10001011110
95 us del 95 195 Ã 10111101000
96 Fnc 3 Fnc 3 96 196 Ä 10111100010
97 Fnc 2 Fnc2 97 197 Å 11110101000
98 Shift Shift 98 198 Æ 11110100010
99 Code C Code C 99 199 Ç 10111011110
100 Code B Fnc 4 Code B 200 È 10111101110
101 Fnc 4 Code A Code A 201 É 11101011110
102 Fnc 1 Fnc 1 Fnc 1 202 Ê 11110101110
103 Start A Start A Start A 203 Ë 11010000100
104 Start B Start B Start B 204 Ì 11010010000
105 Start C Start C Start C 205 Í 11010011100
106 Stop Stop Stop 206 Î 1.10001E+12
var startSymbol = "Ë";
var encodedData = "177345239061003";
var checkSymbol = "";
var stopSymbol = "Î";
// 預設使用 Code Set A ( 128A ) 字元集
Code128Type type = Code128Type.CodeSetA;
// 如果有小寫字元就改用 Code Set B ( 128B ) 字元集
if (Regex.IsMatch(encodedData, "[a-z]"))
{
type = Code128Type.CodeSetB;
}
var checksum = 0;
switch (type)
{
case Code128Type.CodeSetA:
startSymbol = "Ë";
checksum = 103;
break;
case Code128Type.CodeSetB:
startSymbol = "Ì";
checksum = 104;
break;
case Code128Type.CodeSetC:
startSymbol = "Í";
checksum = 105;
break;
default:
throw new ArgumentException("錯誤的 Code128Type 類型", "type");
}
var chars = encodedData.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
var position = i + 1;
var value = 0;
var asciiasc = (int)chars[i];
if (asciiasc >= 32)
{
value = asciiasc - 32;
checksum += value * position;
}
else
{
value = asciiasc + 64;
checksum += value * position;
}
//$"Value: {value}".Dump();
//$"Checksum: {checksum}".Dump();
}
checksum = checksum % 103;
//checksum.Dump();
if (checksum < 95)
{
checkSymbol = char.ConvertFromUtf32(checksum + 32);
}
else
{
checkSymbol = char.ConvertFromUtf32(checksum + 100);
}
var result = startSymbol + encodedData + checkSymbol + stopSymbol;
result.Dump();
public enum Code128Type
{
CodeSetA,
CodeSetB,
CodeSetC
}
Code Value Position Value × position
Start Code A 103 1 103
1 17 1 17
7 23 2 46
7 23 3 69
3 19 4 76
4 20 5 100
5 21 6 126
2 18 7 126
3 19 8 152
9 25 9 225
0 16 10 160
6 22 11 242
1 17 12 204
0 16 13 208
0 16 14 224
3 19 15 285
Sum 2363
Remainder mod 103 97
Local checksum , intPosition, ii , strSpChr , inputData
inputData = TRIM({Source.Id})
checksum = 103
for ii = 0 to Len(inputData)-1
intPosition = ii + 1
strSpChr = Mid(inputData, intPosition, 1)
if Asc(strSpChr) >= 32 then
checksum = checksum + (Asc(strSpChr) - 32) * intPosition
else
checksum = checksum + (Asc(strSpChr) + 64) * intPosition
end if
next
checksum = checksum mod 103
if checksum < 95 then
checksum = checksum + 32
else
checksum = checksum + 100
end if
formula = "Ë" + inputData + chr(checksum) +"Î"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment