银行卡号校验位的LUHN算法模10“隔位2倍加”校验数的公式

 银联卡卡号由三部分组成:发卡机构标识码(bin)、发卡机构自定义位、校验码。其中,卡号左起前六位是发卡机构标识代码(BIN),由6位数字组成。BIN号由中国银联复制分配、确认和管理;卡号第七位起事发卡机构自定义位,由6至12位数字组成,发卡机构自行赋值;卡号最后一位是校验码,根据校验位前的数字(含BIN)取值Luhn方程计算得出。

计算LUHN算法模10“隔位2倍加”校验数的公式


  计算步骤如下:

  步骤1:从右边第1个数字(低序)开始每隔一位乘以2。
  步骤2:把在步骤1中获得的乘积的各位数字与原号码中未乘2的各位数字相加。
  步骤3:从邻近的较高的一个以0结尾的数中减去步骤2中所得到的总和[这相当于求这个总和的低位数字(个位数)的“10的补数”]。如果在步骤2得到的总和是以零结尾的数(如30、40等等),则校验数字就是零。
  例:
    无校验数字的卡号 4992 73 9871 
  4 9  9 2 7 3 9 8 7 1       
   ×2    ×2     ×2    ×2    ×2
   18    4    6   16    2

  4+1+8+9+4+7+6+9+1+6+7+2=64        
  70-64=6                 

  带有校验数字的卡号为:4992 73 9871 6


excel当中的校验公式
:假设卡号在A2单元格中,公式可以计算出账号最后一位的校验位数字

=MOD(SUM(-MID(TEXT(MMULT((0&MID(LEFT(A2,LEN(A2)-1),ROW($1:$9)*2-MOD(LEN(A2)+{0,1},2),1))*{1,2},{100;1}),"000"),{1,2,3},1)),10)

数组公式,要按shift+ctrl+enter结束
求魔方当中的公式应该怎样写呢?
(银联要求的卡号是13-19位此代码要满足13-19位卡号的计算)




答复:


完整支持16-19位银号卡号校验位计算:

text(mod((left(right($MF(账号)$,3),1)+left(right($MF(账号)$,5),1)+left(right($MF(账号)$,7),1)+left(right($MF(账号)$,9),1)+left(right($MF(账号)$,11),1)+left(right($MF(账号)$,13),1)+left(right($MF(账号)$,15),1)+mod(left(right($MF(账号)$,2),1)*2,10)+int(left(right($MF(账号)$,2),1)*2/10)+mod(left(right($MF(账号)$,4),1)*2,10)+int(left(right($MF(账号)$,4),1)*2/10)+mod(left(right($MF(账号)$,6),1)*2,10)+int(left(right($MF(账号)$,6),1)*2/10)+mod(left(right($MF(账号)$,8),1)*2,10)+int(left(right($MF(账号)$,8),1)*2/10)+mod(left(right($MF(账号)$,10),1)*2,10)+int(left(right($MF(账号)$,10),1)*2/10)+mod(left(right($MF(账号)$,12),1)*2,10)+int(left(right($MF(账号)$,12),1)*2/10)+mod(left(right($MF(账号)$,14),1)*2,10)+int(left(right($MF(账号)$,14),1)*2/10)+mod(left(right($MF(账号)$,16),1)*2,10)+int(left(right($MF(账号)$,16),1)*2/10)+if(len($MF(账号)$)>=17,left(right($MF(账号)$,17),1),0)+if(len($MF(账号)$)>=18,mod(value(left(right($MF(账号)$,18),1))*2,10)+int(value(left(right($MF(账号)$,18),1))*2/10),0)+if(len($MF(账号)$)>=19,left(right($MF(账号)$,19),1),0))*9,10),0)

配图

2015-5-5 21:05 上传
下载附件(37.69 KB)
配图



配合银行BIN码的检查,就可以知道银行卡是出自哪个银行。


功德圆满
16-19位的校验位公式
text(mod((left(right($MF(账号)$,3),1)+left(right($MF(账号)$,5),1)+left(right($MF(账号)$,7),1)+left(right($MF(账号)$,9),1)+left(right($MF(账号)$,11),1)+left(right($MF(账号)$,13),1)+left(right($MF(账号)$,15),1)+mod(left(right($MF(账号)$,2),1)*2,10)+int(left(right($MF(账号)$,2),1)*2/10)+mod(left(right($MF(账号)$,4),1)*2,10)+int(left(right($MF(账号)$,4),1)*2/10)+mod(left(right($MF(账号)$,6),1)*2,10)+int(left(right($MF(账号)$,6),1)*2/10)+mod(left(right($MF(账号)$,8),1)*2,10)+int(left(right($MF(账号)$,8),1)*2/10)+mod(left(right($MF(账号)$,10),1)*2,10)+int(left(right($MF(账号)$,10),1)*2/10)+mod(left(right($MF(账号)$,12),1)*2,10)+int(left(right($MF(账号)$,12),1)*2/10)+mod(left(right($MF(账号)$,14),1)*2,10)+int(left(right($MF(账号)$,14),1)*2/10)+mod(left(right($MF(账号)$,16),1)*2,10)+int(left(right($MF(账号)$,16),1)*2/10)+if(len($MF(账号)$)>=17,left(right($MF(账号)$,17),1),0)+if(len($MF(账号)$)>=18,mod(value(left(right($MF(账号)$,18),1))*2,10)+int(value(left(right($MF(账号)$,18),1))*2/10),0)+if(len($MF(账号)$)>=19,left(right($MF(账号)$,19),1),0))*9,10),0)



package com.cup.tracy;

public class Luhn {
	/**
	 * Luhn算法
	 * 根据卡号获取校验位
	 * @param cardNumber
	 * @return
	 */
	public static int getCheckNumber(String cardNumber){
		int totalNumber = 0;
		int lastNumber = 0;
		for (int i = cardNumber.length()-1; i >= 0; i-=2) {
			int tmpNumber = calculate(Integer.parseInt(String.valueOf(cardNumber.charAt(i))) *  2);
			if (i==0) {
				totalNumber += tmpNumber;
			}else {
				totalNumber += tmpNumber + Integer.parseInt(String.valueOf(cardNumber.charAt(i-1)));
			}
			
		}
		if (totalNumber >= 0 && totalNumber < 9) {
			return (10 - totalNumber);
		}else {
			String str = String.valueOf(totalNumber);
			if (Integer.parseInt(String.valueOf(str.charAt(str.length()-1))) == 0) {
				return 0; 
			}else {
				return (10 - Integer.parseInt(String.valueOf(str.charAt(str.length()-1))));
			}
		}
		
	}
	
	/**
	 * 计算数字各位和
	 * @param number
	 * @return
	 */
	public static int calculate(int number){
		String str = String.valueOf(number);
		int total = 0;
		for (int i = 0; i < str.length(); i++) {
			total += Integer.valueOf(Integer.parseInt(String.valueOf(str.charAt(i))));
		}
		return total;
	}
	
	public static void main(String[] args) {
		String cardNumber = "4992739871";
		System.out.println(getCheckNumber(cardNumber));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值