/// <summary>
/// 组织机构代码验证
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
public static Boolean CheckCnCode(string arg)
{
int[] factor = { 3, 7, 9, 10, 5, 8, 4, 2 };//加权因子
if (!Regex.IsMatch(arg, "^[\da-zA-Z]{8}-[\d,x,X]$"))
{
return false;
}
char[] chs = arg.ToUpper().ToCharArray();//全转为大写,放入字符数组
//System.out.println(Arrays.toString(chs));
int sum = 0;
for (int i = 0; i < chs.Length - 2; i++)
{
char chValue = chs[i];
int a = 0;
if (chValue < 65)
{
a = chValue - 48;
}
else
{
a = chValue - 55;
}
//System.out.print(a + "--");
sum += factor[i] * a;
}//转换为数字
int checkNum = 11 - sum % 11;
char checkCh;
if (checkNum == 10)
{
checkCh = 'X';
}
else if (checkNum == 11)
{
checkCh = (char)48;
}
else
{
checkCh = (char)(checkNum + 48);
}//计算验证码 转为 字符
return chs[9] == checkCh;