Created
October 19, 2016 06:06
-
-
Save bagpack/879b27408f32de93a60eab9a9aaaf1b5 to your computer and use it in GitHub Desktop.
Encode alphanumeric in QR code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Encode alphanumeric in QR code | |
''' | |
def encode_alphanum(c): | |
if c >= '0' and c <= '9': | |
return ord(c) - ord('0') | |
elif c >= 'A' and c <= 'Z': | |
return ord(c) - ord('A') + 10 | |
elif c == ' ': | |
return 36 | |
elif c == '$': | |
return 37 | |
elif c == '%': | |
return 38 | |
elif c == '*': | |
return 39 | |
elif c == '+': | |
return 40 | |
elif c == '-': | |
return 41 | |
elif c == '.': | |
return 42 | |
elif c == '/': | |
return 43 | |
elif c == ':': | |
return 44 | |
else: | |
raise Exception('non alphanumeric character %s' % c) | |
data = 'WE LOVE KIMWIPE' | |
blocks = [] | |
for a, b in zip(data[0::2], data[1::2]): | |
blocks.append((a, b)) | |
if len(data) % 2 != 0: | |
blocks.append((None, data[-1])) | |
encoded_nums = [] | |
for block in blocks: | |
if block[0] == None: | |
encoded_nums.append( | |
encode_alphanum(block[1]) | |
) | |
else: | |
encoded_nums.append( | |
encode_alphanum(block[0]) * 45 + encode_alphanum(block[1]) | |
) | |
print(encoded_nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment