2

我正在用 C 语言进行 IBAN 验证。为此,我有一个类似于“2012129431327715102998”的字符*。现在我想通过取模 97 的值来检查 IBAN。所以我想做2012129431327715102998 % 97. 我已经尝试使用 strtoull 转换 char* ,但这给了我一个超出范围的错误。所以我的问题是:如何将此 char* 转换为可以进行模计算的数字?提前致谢

4

2 回答 2

2

一个不使用额外库的简单方法是在数学上记住:mod(a*b, c) == mod(b * mod(a, c), c)。因此,您可以分处理数字:

// suitable for a 32 bits system, can use 8 for a 64 bits one
#define NB 4
/*********************
 * Initial is a string containin only digits representing an arbitrary large number
 * div in a number < 10000 (because NB is 4)
 * ******************/
int large_mod(char *initial, int div) {
    char old[1 + (NB * 2)] = "";   // enough room for a remainder and next chunk
    long val;
    for (unsigned i=0; i<strlen(initial); i+= NB) {
        strncat(old, initial + i, NB);   // add the new chunk
        val = atol(old) % div;           // compute the remainder
        sprintf(old, "%ld", val);        // keep it for next chunk
        // printf("%ld ", val);          // uncomment for debugging
    }
    return (int) val;
}

对于 2012129431327715102998 % 97,它给出了预期的 53。

于 2021-02-22T08:09:53.643 回答
2

您可以为此编写自定义函数。对部分和应用模运算符,您可以转换多个任意长度:

#include <stdio.h>

int mod97(const char *s) {
    int res = 0;
    while (*s >= '0' && *s <= '9') {
        res = (res * 10 + (*s++ - '0')) % 97;
    }
    return res;
}

int main(int argc, char *argv[]) {
    for (int i = 1; i < argc; i++) {
         printf("%s -> %d\n", argv[i], mod97(argv[i]));
    }
    return 0;
}

输出:

./mod97 2012129431327715102998
2012129431327715102998 -> 53

这种方法比wiki文章中描述的方法更简单、更通用:计算一个大数的模 97 可以通过将数字分成 9 位的块并组合这些块的模来实现。这种拆分是特定于97并且有效的,因为1000000000 % 97 == 1. 上述方法适用于最大为 的任何模值INT_MAX / 10

于 2021-02-22T08:19:04.340 回答