2008. június 6., péntek

Italian tax payer code checksum algorithm


Problem/Question/Abstract:

In Italy tax payer code ("codice fiscale") is a 16 digits string identifying every one. The last char is a checksum to simply check if code is valid. There's a simply and rapid function that returns is it's valid.

Answer:

function IsTaxPayer(code: string): boolean;
const
  tables: array[0..1] of string =
    ('A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ',
    'B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX');
var
  i, x: integer;
begin
  result := false;
  code := uppercase(trim(code));
  if (length(code) = 16) then
  begin
    i := 0;
    for x := 1 to 15 do
      i := i + (((pos(code[x], tables[x mod 2]) - 1) and ($7FFE)) div 2);
    result := chr(65 + (i mod 26)) = code[length(code)];
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése