feat: end-point for deal document generation
This commit is contained in:
35
utils/code128.py
Normal file
35
utils/code128.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import re
|
||||
|
||||
def to_set_c(text):
|
||||
# Match pairs of digits and map them to the appropriate ASCII characters
|
||||
return ''.join([
|
||||
chr(int(ascii_code) + 100 if int(ascii_code) > 94 else int(ascii_code) + 32)
|
||||
for ascii_code in re.findall(r'\d{2}', text)
|
||||
])
|
||||
|
||||
def check_sum_128(data, start_code):
|
||||
sum_value = start_code
|
||||
for i, char in enumerate(data):
|
||||
code = ord(char)
|
||||
value = code - 100 if code > 199 else code - 32
|
||||
sum_value += (i + 1) * value
|
||||
|
||||
checksum = (sum_value % 103) + 32
|
||||
if checksum > 126:
|
||||
checksum += 68
|
||||
return chr(checksum)
|
||||
|
||||
def encode128(text, code_abc="B"):
|
||||
start_code = chr(ord(code_abc.upper()) + 138)
|
||||
stop = chr(206)
|
||||
|
||||
if code_abc.upper() == 'C':
|
||||
text = to_set_c(text)
|
||||
|
||||
check = check_sum_128(text, ord(start_code) - 100)
|
||||
|
||||
# Replace spaces with ASCII 194
|
||||
text = text.replace(" ", chr(194))
|
||||
|
||||
return start_code + text + check + stop
|
||||
|
||||
Reference in New Issue
Block a user