Created
September 15, 2011 09:05
-
-
Save iserko/1218866 to your computer and use it in GitHub Desktop.
Croatian (Hrvatski) IBAN control number checker
This file contains 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
#!/usr/bin/env python | |
""" | |
This script is primarily used to get the IBAN number for Croatian account numbers | |
""" | |
import os | |
import sys | |
def get_iso13616_numbers(chars): | |
out = "" | |
chars = str(chars) | |
for char in chars: | |
char = char.upper() | |
char_num = ord(char) | |
out += str(char_num-55) # capital A is 65 and equals 10 in ISO13616 | |
return out | |
def get_iban_number(country, account_number): | |
number = str(account_number) | |
number += get_iso13616_numbers(country) | |
number += "00" | |
assert(len(number) == 23) | |
int_num = int(number) | |
remainder = int_num % 97 | |
control = 97 + 1 - remainder | |
if control < 10: | |
control = "0" + str(control) | |
control = str(control) | |
print "Control number = %s" % control | |
test = str(account_number) | |
test += get_iso13616_numbers(country) | |
test += control | |
int_test = int(test) | |
print "Check = %s (should be 1)" % (int_test % 97) | |
if int_test % 97 != 1: | |
print "Test error" | |
else: | |
print "Test OK" | |
print "Full IBAN = %s%s%s" % (country, str(control), str(account_number)) | |
if __name__ == '__main__': | |
if len(sys.argv) == 3: | |
country = sys.argv[1] | |
number = sys.argv[2] | |
sys.exit(get_iban_number(country, number)) | |
else: | |
sys.stderr.write("Not enough arguments ... usage: %s <country> <account_number>\n" % sys.argv[0]) | |
sys.exit(254) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment