本文介绍了IBAN验证程序Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一种算法来验证Swift 3中的IBAN(国际银行帐号),但无法找到其中一种验证方式.
I am writing an algorithm to validate IBAN (International Bank Account Number) in Swift 3 and not able to figure one of the validation.
示例IBAN-BE68539007547034
Example IBAN - BE68539007547034
以下是验证规则-
- 输入数字的长度应为16.
- 前2个字符是国家/地区代码(不是数字).
- 最后14个数字.
- 最后2个字符是前12个数字字符的模97结果.
虽然#1-#3很清楚,但我需要在#4上保持清晰.如果有人以前曾经做过此事并且对此有所了解,请告诉我.
While #1 - #3 are clear I need clarity on #4. If anyone have done this before and know about it then please let me know.
推荐答案
来自 Wikipedia
let IBAN = "GB82WEST12345698765432" // uppercase, no whitespace !!!!
var a = IBAN.utf8.map{ $0 }
while a.count < 4 {
a.append(0)
}
let b = a[4..<a.count] + a[0..<4]
let c = b.reduce(0) { (r, u) -> Int in
let i = Int(u)
return i > 64 ? (100 * r + i - 55) % 97: (10 * r + i - 48) % 97
}
print( "IBAN \(IBAN) is", c == 1 ? "valid": "invalid")
打印
IBAN GB82WEST12345698765432 is valid
使用您问题中的IBAN进行打印
With IBAN from your question it prints
IBAN BE68539007547034 is valid
这篇关于IBAN验证程序Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!