2016-05-07 5 views
-2

Umwandlung kann ich nicht scheinen, dies, nachdem es auf C#Ungültige Basisfehler, wenn Java Base-27-Umwandlungsfunktion auf C#

Umwandlung zu arbeiten

ich es brauche, wie die Java ein zu arbeiten, aber ich kann nicht scheinen zu in der Lage sein, das zu tun, was mache ich falsch?

Kann jemand eine Reparatur liefern und es bitte erklären?

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; 
} 

Java:

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; 
} 

Der Fehler:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll 

Additional information: Invalid Base. 

Fehlerzeile:

strOut += Convert.ToString((int)(strIn[i] + (char)numRand), 27) + 
      Convert.ToString((int)numRand, 27); 
+1

'numRand = Math.floor (Math.random() * 66) + 36;' sagt 36 in Java und 33 in C# –

+0

i geändert sehen, aber noch nicht das gleiche Ergebnis ty. – Punxor

+0

Was genau versuchen Sie zu erreichen und welche Fehler bekommen Sie? – galacticfan

Antwort

-2

Das funktioniert mit Basis 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; 
} 
+0

Hinzugefügt die Fehlerzeile, die ich nicht downvote. – Punxor

+0

Ahh, also das wirft eine Ausnahme .. Lass mich das überprüfen. –

+0

@Puxnor, dies funktioniert mit Basis 16. Siehe StuartLCs Kommentar. –

Verwandte Themen