以下代码为国家身份证的校验函数。由于计划把此函数应用到SSIS的Package Script里,所以只能用VB.NET写了。代码是根据PB版本的代码翻译过来的,所以有些逻辑不符合OO的思想,请谅解。
1 Imports System
2
3 Public Class SFZ
4 Private _code As String
5
6 Public Sub New(ByVal code As String)
7 _code = code
8 End Sub
9
10 Public Function Verify() As Boolean
11 '返回结果
12 Dim result As Boolean = False
13 Dim datestr As System.Text.StringBuilder = New System.Text.StringBuilder
14
15 Select Case _code.Length
16 Case 15
17 '取得日期字符串
18 datestr.Append("19")
19 datestr.Append(_code.Substring(6, 2))
20 datestr.Append("-")
21 datestr.Append(_code.Substring(8, 2))
22 datestr.Append("-")
23 datestr.Append(_code.Substring(10, 2))
24 '判断是否为日期
25 result = IsDate(datestr.ToString())
26 Case 18
27 '取得日期字符串
28 datestr.Append(_code.Substring(6, 4))
29 datestr.Append("-")
30 datestr.Append(_code.Substring(10, 2))
31 datestr.Append("-")
32 datestr.Append(_code.Substring(12, 2))
33 '判断是否为日期
34 result = IsDate(datestr.ToString())
35 If result Then
36 '判断校验码是否正确
37 If _code.Substring(17, 1) = lastCodeVerify(_code.Substring(0, 17)) Then
38 result = True
39 Else
40 result = False
41 End If
42 End If
43 Case Else
44 result = False
45 End Select
46
47 Verify = result
48 End Function
49
50 Private Function lastCodeVerify(ByVal a_sfz As String) As String
51 '获得校验码
52 Dim lastcode As String
53 Dim ls_sfz As String
54 Dim i, li_ai, li_wi, li_sum, li_mod, li_result As Integer
55
56 ls_sfz = a_sfz
57 If ls_sfz.Length = 15 Or ls_sfz.Length = 17 Then
58 If ls_sfz.Length = 15 Then
59 '应该不会执行
60 ls_sfz = ls_sfz.Substring(0, 6) + "19" + ls_sfz.Substring(6, 9)
61 End If
62
63 For i = 2 To 18
64 li_ai = CInt(ls_sfz.Substring(19 - i - 1, 1))
65 li_wi = System.Math.Pow(2, i - 1) Mod 11
66 li_sum = li_sum + li_ai * li_wi
67 Next
68
69 li_mod = li_sum Mod 11
70 li_result = 12 - li_mod
71
72 If li_result >= 10 Then
73 If li_result = 10 Then
74 lastcode = "X"
75 Else
76 li_result = li_result - 11
77 lastcode = CStr(li_result)
78 End If
79 Else
80 lastcode = CStr(li_result)
81 End If
82 Else
83 lastcode = "E"
84 End If
85
86 lastCodeVerify = lastcode
87
88 End Function
89 End Class
2
3 Public Class SFZ
4 Private _code As String
5
6 Public Sub New(ByVal code As String)
7 _code = code
8 End Sub
9
10 Public Function Verify() As Boolean
11 '返回结果
12 Dim result As Boolean = False
13 Dim datestr As System.Text.StringBuilder = New System.Text.StringBuilder
14
15 Select Case _code.Length
16 Case 15
17 '取得日期字符串
18 datestr.Append("19")
19 datestr.Append(_code.Substring(6, 2))
20 datestr.Append("-")
21 datestr.Append(_code.Substring(8, 2))
22 datestr.Append("-")
23 datestr.Append(_code.Substring(10, 2))
24 '判断是否为日期
25 result = IsDate(datestr.ToString())
26 Case 18
27 '取得日期字符串
28 datestr.Append(_code.Substring(6, 4))
29 datestr.Append("-")
30 datestr.Append(_code.Substring(10, 2))
31 datestr.Append("-")
32 datestr.Append(_code.Substring(12, 2))
33 '判断是否为日期
34 result = IsDate(datestr.ToString())
35 If result Then
36 '判断校验码是否正确
37 If _code.Substring(17, 1) = lastCodeVerify(_code.Substring(0, 17)) Then
38 result = True
39 Else
40 result = False
41 End If
42 End If
43 Case Else
44 result = False
45 End Select
46
47 Verify = result
48 End Function
49
50 Private Function lastCodeVerify(ByVal a_sfz As String) As String
51 '获得校验码
52 Dim lastcode As String
53 Dim ls_sfz As String
54 Dim i, li_ai, li_wi, li_sum, li_mod, li_result As Integer
55
56 ls_sfz = a_sfz
57 If ls_sfz.Length = 15 Or ls_sfz.Length = 17 Then
58 If ls_sfz.Length = 15 Then
59 '应该不会执行
60 ls_sfz = ls_sfz.Substring(0, 6) + "19" + ls_sfz.Substring(6, 9)
61 End If
62
63 For i = 2 To 18
64 li_ai = CInt(ls_sfz.Substring(19 - i - 1, 1))
65 li_wi = System.Math.Pow(2, i - 1) Mod 11
66 li_sum = li_sum + li_ai * li_wi
67 Next
68
69 li_mod = li_sum Mod 11
70 li_result = 12 - li_mod
71
72 If li_result >= 10 Then
73 If li_result = 10 Then
74 lastcode = "X"
75 Else
76 li_result = li_result - 11
77 lastcode = CStr(li_result)
78 End If
79 Else
80 lastcode = CStr(li_result)
81 End If
82 Else
83 lastcode = "E"
84 End If
85
86 lastCodeVerify = lastcode
87
88 End Function
89 End Class
浙公网安备 33010602011771号