`
javazhou
  • 浏览: 68189 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

身份证最后一位权值计算

 
阅读更多

 

public class BinaryTest
{
    private static int[] weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

    private static int[] check = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };


    public static void main(String[] args)
    {
        System.out.println(getVerifyCode("42108719881002761"));
    }


    public static String getVerifyCode(String idcard)
    {
        String result = null;
        int sum = 0;
        for (int i = 0; i < idcard.length(); i++)
        {
            sum += Integer.parseInt(idcard.substring(i, i + 1)) * weight[i];
        }
        result = (sum % 11) == 2 ? "X" : String.valueOf(check[sum % 11]);
        return result;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics