Java 如何计算 IBAN 国家校验位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19881332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:00:29  来源:igfitidea点击:

How can I calculate an IBAN national check digit?

javacheck-digitiban

提问by Tostis

Following Wikipedia, I'm developing a Java application that calculates and verifies IBANs from various countries. Some BBAN have a national check digit, but I can't find any documentation about how to calculate it. Where I can find the algorithm for "national check digit"? I'm not interested in "iban check digit" but on the country related one.

Wikipedia之后,我正在开发一个 Java 应用程序,用于计算和验证来自不同国家/地区的 IBAN。一些 BBAN 有一个国家校验码,但我找不到任何关于如何计算它的文档。我在哪里可以找到“国家校验位”的算法?我对“iban 校验位”不感兴趣,但对与国家相关的数字不感兴趣。

采纳答案by Joni

The national check digit is defined in national standards, each country having a different standard. Some countries have one check digit, others have two, yet others don't have any.

国家校验位在国家标准中定义,每个国家都有不同的标准。有些国家有一个校验位,有些国家有两个,还有一些国家没有。

Spanish bank account numbers for example have two check digits. The first is based on the 4-digit branch and office codes, and the second one is computed from the 10-digit account number. You can find it documented in any document related to banking IT, for example the ones here, but basically you multiply each digit by a power of 2 mod 11, sum the resulting digits together, and take its remainder when divided by 11. The Wikipedia entryhas example code for verifying and computing the check digits.

例如,西班牙银行帐号有两个校验位。第一个是基于 4 位分行和办公室代码,第二个是根据 10 位帐号计算的。您可以在与银行 IT 相关的任何文档中找到它,例如此处的文档,但基本上您将每个数字乘以 2 mod 11 的幂,将所得数字相加,然后除以 11 时取余数。维基百科条目具有用于验证和计算校验位的示例代码。

Other countries use other methods, for example the Luhn algorithm.

其他国家使用其他方法,例如Luhn 算法

回答by user3364216

When you need to construct/deconstruct the Spanish Iban, spain is a country which has two sets of check digits, calculated in the order below: 45, 91

当您需要构建/解构西班牙伊班语时,西班牙是一个有两组校验位的国家,按以下顺序计算:45、91

ES91 2100 0418 4502 0005 1332

ES91 2100 0418 4502 0005 1332

Countrycode (2 characters 'ES') Check digits (2 characters) Bank identifier (4 characters) Branch code (4 characters) check digit (2 characters) Account number (10 characters)

国家/地区代码(2 个字符“ES”) 校验位(2 个字符) 银行标识符(4 个字符) 分行代码(4 个字符) 校验位(2 个字符) 帐号(10 个字符)

Step-by-step guide 1. First, change the ES to its numeric equivalent E=14, S=28 and append this value with 00, so 142800 Now, to get the first set of iban check digits, add account number to the number from step 1: 0200051332142800

分步指南 1. 首先,将 ES 更改为其等效的数字 E=14, S=28 并将此值附加 00,即 142800 现在,要获取第一组 iban 校验位,请将帐号添加到第 1 步的编号:0200051332142800

  1. Now mod 97 that value, then subtract the remainder from 98. 0200051332142800 mod 97 = 53, 98 - 53 = 45 <- first set of check digits! (smile)

  2. If you end up with one digit, it should be padded with a leading zero.

  3. To get the second iban check digits, append the bank id(2100) to the branch id(0418) then account number, plus first check digits (450200051332)then the value from step 1, in total: 21000418450200051332142800

  4. Again mod 97 that value, 21000418450200051332 mod 97 = 7, 98 -7 = 91 <- second set of check digits (smile)

  1. 现在 mod 97 该值,然后从 98 中减去余数。 0200051332142800 mod 97 = 53, 98 - 53 = 45 <- 第一组校验位!(微笑)

  2. 如果您最终得到一位数字,则应该用前导零填充它。

  3. 要获得第二个 iban 校验位,将银行 ID(2100)附加到分行 ID(0418)然后是帐号,加上第一个校验位(450200051332)然后是步骤 1 中的值,总共:21000418450200051332142800

  4. 再次 mod 97 该值,21000418450200051332 mod 97 = 7, 98 -7 = 91 <- 第二组校验位(微笑)