Thread ISIN check in Perl (12 answers)
Opened by lichtkind at 2009-10-26 02:39

esskar
 2009-10-30 10:44
#127572 #127572
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
hier mal meine Version in c#
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
public sealed class Isin
{
private string m_cc, m_nsin;
private int m_digest;

public static bool LooksLike(string isin)
{
if (string.IsNullOrEmpty(isin))
return false;
isin = isin.Trim();
if (string.IsNullOrEmpty(isin))
return false;
Isin obj;
return Isin.TryParse(isin, out obj);
}

public static bool TryParse(string isin, out Isin obj)
{
bool retval = false;

Isin i = new Isin();
if (retval = i.ParseInternal(isin)) obj = i;
else obj = null;

return retval;
}

public static Isin Parse(string isin)
{
return new Isin(isin);
}

public Isin(string isin)
{
if (!this.ParseInternal(isin))
throw new FormatException("Not a isin.");
}

private Isin() { }

private bool ParseInternal(string isin)
{
bool retval = false;
do
{
isin = isin.Trim().ToUpperInvariant();
if (isin.Length != 12)
break;
retval = isin.StartsWith("IX") && isin.EndsWith("X"); // special case
if (retval || (retval = (Isin.DoubleAddDouble(isin) == 0)))
{
m_cc = isin.Substring(0, 2);
m_nsin = isin.Substring(1, 10);
if (!int.TryParse(isin.Substring(11), out m_digest))
m_digest = int.MaxValue;
}
}
while(false);

return retval;
}

private static int DoubleAddDouble(string src)
{
int s = 0;
int a = src.Length == 12 ? 1 : 2;
for (int i = src.Length - 1; i >= 0; i--)
{
int c = src[i];
if (c > '9') // letter
{
c -= ('A' - 10);
s += (3 - a) * (c / 10) + a * c + (a - 1) * (c % 10) / 5;
}
else // number
{
c -= '0';
s += a * c + (a - 1) * (c / 5);
a = 3 - a;
}
}
s %= 10;
return (10 - s % 10) % 10;
}

public string CountryCode
{
get { return m_cc; }
}

public string Nsin
{
get { return m_nsin; }
}

public int Digest
{
get { return m_digest; }
}

public override string ToString()
{
if (this.Digest == int.MaxValue)
return string.Format("{0}{1}X", this.CountryCode, this.Nsin);
else
return string.Format("{0}{1}{2}", this.CountryCode, this.Nsin, this.Digest);
}
}

View full thread ISIN check in Perl