C# - Classi utili - Codice fiscale
Classe per verificare il Codice Fiscale
• La classe consente di calcolare il 16° carattere del Codice Fiscale a partire dai primi 15 caratteri.
• Questo carattere è chiamato 'Carattere di controllo' e il suo calcolo è fatto seguendo l'algoritmo seguente.
Algoritmo per il calcolo del Codice di controllo
• Si assegnano ai caratteri, i corrispondenti valori contenuti nella seguente tabella; i valori differiscono in base al fatto se il carattere in esame è in posizione dispari o in posizione pari.
Carattere |
Posizione |
Posizione |
|
A e 0 |
1 |
0 |
|
B e 1 |
0 |
1 |
|
C e 2 |
5 |
2 |
|
D e 3 |
7 |
3 |
|
E e 4 |
9 |
4 |
|
F e 5 |
13 |
5 |
|
G e 6 |
15 |
6 |
|
H e 7 |
17 |
7 |
|
I e 8 |
19 |
8 |
|
J e 9 |
21 |
9 |
|
K |
2 |
10 |
|
L |
4 |
11 |
|
M |
18 |
12 |
|
N |
20 |
13 |
|
O |
11 |
14 |
|
P |
3 |
15 |
|
Q |
6 |
16 |
|
R |
8 |
17 |
|
S |
12 |
18 |
|
T |
14 |
19 |
|
U |
16 |
20 |
|
V |
10 |
21 |
|
W |
22 |
22 |
|
X |
25 |
23 |
|
Y |
24 |
24 |
|
Z |
23 |
25 |
• Si sommano tutti i valori ottenuti dai primi 15 caratteri del Codice Fiscale.
• Il numero ottenuto lo si divide per 26 e si considera il resto della divisione.
• Il Codice di controllo si ricava dalla seguente tabella.
Resto |
Codice di controllo |
|
0 |
A |
|
1 |
B |
|
2 |
C |
|
3 |
D |
|
4 |
E |
|
5 |
F |
|
6 |
G |
|
7 |
H |
|
8 |
I |
|
9 |
J |
|
10 |
K |
|
11 |
L |
|
12 |
M |
|
13 |
N |
|
14 |
O |
|
15 |
P |
|
16 |
Q |
|
17 |
R |
|
18 |
S |
|
19 |
T |
|
20 |
U |
|
21 |
V |
|
22 |
W |
|
23 |
X |
|
24 |
Y |
|
25 |
Z |
La classe 'Codice Fiscale'
• Questa è la classe 'CodiceFiscale'.
• La classe è composta dal metodo pubblico 'VerificaCodiceFiscale'; esso accetta in input un Codice Fiscale di 16 caratteri e restituisce True/False se rispettivamente il codice è formalmente corretto e se non lo è.
• Sono anche presenti i metodi privati 'CalcolaCodiceControllo' e 'ValoreDelCarattere' il cui significato è commentato nel codice.
public static bool VerificaCodiceFiscale(string Codice)
{
// Dato un Codice Fiscale verifica il codice di controllo
// Input: il Codice Fiscale da verificare, 16 caratteri
// Output: true/false
// 2010.12.05
Codice = Codice.ToUpper();
if (Codice.Length != 16)
return false; // errore
else
{
if (Codice.Substring(15, 1) ==
CalcolaCodiceControllo(Codice.Substring(0, 15)))
return true;
else
return false;
}
}
private static string CalcolaCodiceControllo(string Codice)
{
// Calcola il codice di controllo del Codice Fiscale
// Input: i primi 15 caratteri del Codice Fiscale
// Output: il codice di controllo
// 2010.12.05
int Contatore = 0;
Codice = Codice.ToUpper();
if (Codice.Length != 15)
return "0"; // zero: errore
else
{
for (int i = 0; i < Codice.Length; i++)
{
Contatore += ValoreDelCarattere(Codice.Substring(i, 1), i);
}
Contatore %= 26; // si considera il resto
return Convert.ToChar(Contatore + 65).ToString();
}
}
private static int ValoreDelCarattere(string Carattere, int Posizione)
{
int Valore = 0;
switch (Carattere)
{
case "A":
case "0":
if ((Posizione % 2) == 0)
Valore = 1;
else
Valore = 0;
break;
case "B":
case "1":
if ((Posizione % 2) == 0)
Valore = 0;
else
Valore = 1;
break;
case "C":
case "2":
if ((Posizione % 2) == 0)
Valore = 5;
else
Valore = 2;
break;
case "D":
case "3":
if ((Posizione % 2) == 0)
Valore = 7;
else
Valore = 3;
break;
case "E":
case "4":
if ((Posizione % 2) == 0)
Valore = 9;
else
Valore = 4;
break;
case "F":
case "5":
if ((Posizione % 2) == 0)
Valore = 13;
else
Valore = 5;
break;
case "G":
case "6":
if ((Posizione % 2) == 0)
Valore = 15;
else
Valore = 6;
break;
case "H":
case "7":
if ((Posizione % 2) == 0)
Valore = 17;
else
Valore = 7;
break;
case "I":
case "8":
if ((Posizione % 2) == 0)
Valore = 19;
else
Valore = 8;
break;
case "J":
case "9":
if ((Posizione % 2) == 0)
Valore = 21;
else
Valore = 9;
break;
case "K":
if ((Posizione % 2) == 0)
Valore = 2;
else
Valore = 10;
break;
case "L":
if ((Posizione % 2) == 0)
Valore = 4;
else
Valore = 11;
break;
case "M":
if ((Posizione % 2) == 0)
Valore = 18;
else
Valore = 12;
break;
case "N":
if ((Posizione % 2) == 0)
Valore = 20;
else
Valore = 13;
break;
case "O":
if ((Posizione % 2) == 0)
Valore = 11;
else
Valore = 14;
break;
case "P":
if ((Posizione % 2) == 0)
Valore = 3;
else
Valore = 15;
break;
case "Q":
if ((Posizione % 2) == 0)
Valore = 6;
else
Valore = 16;
break;
case "R":
if ((Posizione % 2) == 0)
Valore = 8;
else
Valore = 17;
break;
case "S":
if ((Posizione % 2) == 0)
Valore = 12;
else
Valore = 18;
break;
case "T":
if ((Posizione % 2) == 0)
Valore = 14;
else
Valore = 19;
break;
case "U":
if ((Posizione % 2) == 0)
Valore = 16;
else
Valore = 20;
break;
case "V":
if ((Posizione % 2) == 0)
Valore = 10;
else
Valore = 21;
break;
case "W":
if ((Posizione % 2) == 0)
Valore = 22;
else
Valore = 22;
break;
case "X":
if ((Posizione % 2) == 0)
Valore = 25;
else
Valore = 23;
break;
case "Y":
if ((Posizione % 2) == 0)
Valore = 24;
else
Valore = 24;
break;
case "Z":
if ((Posizione % 2) == 0)
Valore = 23;
else
Valore = 25;
break;
default:
Valore = 0;
break;
}
return Valore;
}
}