来自非常大的int C#的模数
|
我遇到了来自31个字符的整数模的问题。似乎错了
Int64 convertedNumber = Int64.Parse(mergedNumber);
和Value was either too large or too small for an Int64. (Overflow Exception)
。如何解决这个问题,以免模数出错?
class GeneratorRachunkow {
private static string numerRozliczeniowyBanku = \"11111155\"; // 8 chars
private static string identyfikatorNumeruRachunku = \"7244\"; // 4 chars
private static string stalaBanku = \"562100\"; // 6 chars
public static string generator(string pesel, string varKlientID) {
string peselSubstring = pesel.Substring(pesel.Length - 5); // 5 chars (from the end of the string);
string toAttach = varKlientID + peselSubstring;
string indywidualnyNumerRachunku = string.Format(\"{0}\", toAttach.ToString().PadLeft(13, \'0\')); // merging pesel with klient id and adding 0 to the begining to match 13 chars
string mergedNumber = numerRozliczeniowyBanku + identyfikatorNumeruRachunku + indywidualnyNumerRachunku + stalaBanku; // merging everything -> 31 chars
Int64 convertedNumber = Int64.Parse(mergedNumber);
Int64 modulo = MathMod(convertedNumber, 97);
Int64 wynik = 98 - modulo;
string wynikString = string.Format(\"{0}\", wynik.ToString().PadLeft(2, \'0\')); // must be 2 chars
indywidualnyNumerRachunku = wynikString + numerRozliczeniowyBanku + identyfikatorNumeruRachunku + indywidualnyNumerRachunku;
return indywidualnyNumerRachunku;
}
private static Int64 MathMod(Int64 a, Int64 b) {
return (Math.Abs(a * b) + a) % b;
}
}
没有找到相关结果
已邀请:
4 个回复
屠创氓读叔
的最大值为
(打印时为19个字符)。您可能需要改用
(在.NET 4中引入):
席酱
拈吉勉犬姆
(由于您所有的数字都是正数,因此像原始的MathMod函数中那样使用Math.Abs没有意义)。 使用这种方式:
自1.1版以来,此版本应可用于所有版本的.NET,而无需BigInteger。
蹄寄噬非芹