From 940c8db1b5a0891f1a87b193cdfb9e698eaefcba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com>
Date: Mon, 20 Apr 2026 01:10:38 +0800
Subject: [PATCH] *
---
General/MathUtils.cs | 52 ++++++++++++++++++++++++--------------------
1 file changed, 29 insertions(+), 23 deletions(-)
diff --git a/General/MathUtils.cs b/General/MathUtils.cs
index 57b3ef3..2c077ec 100644
--- a/General/MathUtils.cs
+++ b/General/MathUtils.cs
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Numerics;
namespace MeloongCore {
public static class MathUtils {
@@ -21,48 +22,53 @@ namespace MeloongCore {
bool isNegative = input.StartsWithF("-");
if (isNegative) input = input.TrimStart('-');
// 转换为十进制
- long realNum = 0; long scale = 1;
- foreach (var digit in input.Reverse().Select(l => digits.IndexOf(l))) {
- realNum += digit * scale;
- scale *= fromRadix;
+ BigInteger realNum = 0;
+ foreach (char c in input) {
+ int digit = digits.IndexOf(c);
+ if (digit == -1 || digit >= fromRadix) throw new ArgumentException($"Character '{c}' in input '{input}' is not a valid digit for radix {fromRadix}.");
+ realNum = realNum * fromRadix + digit;
}
- return RadixConvert(realNum * (isNegative ? -1 : 0), toRadix);
+ return RadixConvert(realNum * (isNegative ? -1 : 1), toRadix);
}
///
/// 转换为 2 到 86 进制的字符串。
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
///
public static string RadixConvert(this long input, int toRadix) {
- const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
- if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
- // 零与负数的预处理
- if (input == 0) return "0";
- bool isNegative = input < 0;
- if (isNegative) input = -input;
- // 转换为指定进制
- var results = new List();
- while (input > 0) {
- int newNum = (int) (input % toRadix);
- input = (input - newNum) / toRadix;
- results.Add(digits[newNum]);
- }
- // 负数的结束处理与返回
- if (isNegative) results.Add('-');
- return new string(results.ToArray().Reverse().ToArray());
+ return RadixConvert((BigInteger) input, toRadix);
}
///
/// 转换为 2 到 86 进制的字符串。
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
///
public static string RadixConvert(this int input, int toRadix) {
- return RadixConvert((long) input, toRadix);
+ return RadixConvert((BigInteger) input, toRadix);
}
///
/// 转换为 2 到 86 进制的字符串。
/// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86
///
public static string RadixConvert(this byte input, int toRadix) {
- return RadixConvert((long) input, toRadix);
+ return RadixConvert((BigInteger) input, toRadix);
+ }
+ private static string RadixConvert(this BigInteger input, int toRadix) {
+ const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',";
+ if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86.");
+ // 零与负数的预处理
+ if (input == 0) return "0";
+ bool isNegative = input < 0;
+ // 转换为指定进制
+ var results = new List();
+ BigInteger remaining = BigInteger.Abs(input);
+ BigInteger bigRadix = toRadix;
+ while (remaining > 0) {
+ remaining = BigInteger.DivRem(remaining, bigRadix, out BigInteger remainder);
+ results.Add(digits[(int) remainder]);
+ }
+ // 负数的结束处理与返回
+ if (isNegative) results.Add('-');
+ results.Reverse();
+ return new string(results.ToArray());
}
#endregion