[英]Invalid Base error when converting Java Base 27 Conversion function to C#
將其轉換為 C# 后,我似乎無法讓它工作
我需要它像 Java 一樣工作,但我似乎無法做到這一點,我做錯了什么?
任何人都可以提供修復並解釋一下嗎?
C#:
public static string Encrypt1(string strIn)
{
string strOut = "";
int lenIn = strIn.Length;
int i = 0;
while (i < lenIn)
{
double numRand = (int)Math.Floor(new Random().NextDouble() * 66) + 36;
strOut += Convert.ToString((int)(strIn[i] + (char)numRand), 27) +
Convert.ToString((int)numRand, 27);
i++;
}
return strOut;
}
爪哇:
public String Encrypt1(String strIn) {
String strOut = "";
int lenIn = strIn.length();
int i = 0;
double numRand;
while (i < lenIn) {
numRand = Math.floor(Math.random() * 66) + 36;
strOut += Integer.toString((int)(strIn.charAt(i) + (char)numRand), 27) +
Integer.toString((int)numRand, 27);
i++;
}
return strOut;
}
錯誤:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Invalid Base.
錯誤行:
strOut += Convert.ToString((int)(strIn[i] + (char)numRand), 27) +
Convert.ToString((int)numRand, 27);
這適用於基數 16。
public static string Encrypt1(string strIn)
{
int radix = 16;
string strOut = "";
int lenIn = strIn.Length;
int i = 0;
double numRand;
Random random = new Random();
while (i < lenIn)
{
numRand = (int)Math.Floor(random.NextDouble() * 66) + 36;
strOut += Convert.ToString((strIn[i] + (int)numRand), radix) +
Convert.ToString((int)numRand, radix);
i++;
}
return strOut;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.