-2

I can't seem to get this to work after converting it to C#

I need it to work like the Java one but I can't seem to be able to do that what am I doing wrong?

Can anyone supply a fix and explain it please?

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

The error:

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

Additional information: Invalid Base.

Error line:

   strOut += Convert.ToString((int)(strIn[i] + (char)numRand), 27) +
            Convert.ToString((int)numRand, 27);
StuartLC
  • 104,537
  • 17
  • 209
  • 285
Punxor
  • 27
  • 7

1 Answers1

-2

This works with base 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;
}
Tim Ashton
  • 436
  • 6
  • 18