Skip to content

Instantly share code, notes, and snippets.

@stfnndrsn
Created August 24, 2018 14:55
Show Gist options
  • Save stfnndrsn/1b244e35b2d3cf261417e80ca26d4f55 to your computer and use it in GitHub Desktop.
Save stfnndrsn/1b244e35b2d3cf261417e80ca26d4f55 to your computer and use it in GitHub Desktop.
def _calculate_ocr_checksum(ocr_code):
"""
Inspired from https://danskebank.dk/PDF/Blanketter/ERHVERV/Indbetalingskort/Fremstilling_af_71-kort.pdf
:param ocr_code: string
:return: int
"""
counter = 0
should_multiply = (len(ocr_code) % 2) == 1
for c in ocr_code:
v = int(c) * 2 if should_multiply else int(c)
counter += sum(map(int, str(v))) # sum of digits
should_multiply = not should_multiply
return 10 - (counter % 10)
@ManuelZierl
Copy link

be aware that this code will return '10' instead of '0'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment