- 论坛徽章:
- 0
|
最近在做一个项目,其中涉及到支付系统12位行号的生成。根据人总行制定的业务标准,支付系统银行行号为12位定长数字,由3位银行行别代码、4位地区代码、4位分支机构序号和1位校验码组成。格式如下:
XXX XXXX XXXX X
行别代码 地区代码 分支机构序号 校验码
其中检验码为1位数字,使用模10、11双模算法得出。
经查阅资料,文中所说“使用模10、11双模算法”是指的ISO 7064 MOD 11,10,国内标准为GB/T 17710-1999。该标准用于对数字串进行运算,得到一位校验码。
按照标准中给出的算法,具体的实现代码如下:- #include <stdio.h>
- #include <stdlib.h>
- int gen( char *s, int n )
- {
- const char *a = s;
- unsigned P = 10;
- unsigned S;
- int j;
- for( j = 0; j < n; j++ )
- {
- if (!isdigit(*a))
- return -1;
- S = P + (*a++ - '0');
- if( (S=S%10) == 0 )
- S = 10;
- P = (S*2)%11;
- };
- return( (11-P)%10 );
- }
复制代码 参数s为用于生成校验位的数字串、n为待校验位的长度。函数返回值为校验位值。
当用于校验时,仅需要对以上代码稍作修改:- #include <stdio.h>
- #include <stdlib.h>
- int check( char *s, int n )
- {
- const char *a = s;
- unsigned P = 10;
- unsigned S;
- int j;
- for( j = 0; j < n-1; j++ )
- {
- if (!isdigit(*a))
- return -1;
- S = P + (*a++ - '0');
- if( (S=S%10) == 0 )
- S = 10;
- P = (S*2)%11;
- };
- return( (P+s[n-1]-'0')%10 == 1 ? 1 : 0 );
- }
复制代码 其中,s为含校验码的数字串、n为串的长度。
|
|