Java Integer.ValueOf method equivalence in C # with Radix parameter

My task is to port this Java code to the C # version, but I am having problems with the method ValueOf, since I cannot find the equivalent version for C # (due to the parameter Radixused in Java, 16 in this case).

public String decrypt_string(String s) 
{
  String s1 = "";
  int i = s.length() / 2;
  int[] ai = new int[i];

  for (int j = 0; j < i; j++) 
  {
    // This is the "problematic" line \/
    ai[j] = Integer.valueOf(s.substring(j * 2, j * 2 + 2), 16).intValue();
  }

  int[] ai1 = decrypt_block(ai, i);

  for (int k = 0; k < i; k++) 
  {
    if (ai1[k] != 0)
      s1 = s1 + (char)ai1[k];
  }

return s1;

}

Here is my attempt, but it failed:

public String decrypt_string(String s)
    {
        String s1 = "";
        int i = s.Length / 2;
        int[] ai = new int[i];

        for (int j = 0; j < i; j++)
        {
            int startIndex = j * 2;
            string tmp = s.Substring(startIndex, 2);
            ai[j] = Int32.Parse (tmp); 
        }

        int[] ai1 = decrypt_block(ai, i);

        for (int k = 0; k < i; k++)
        {
            if (ai1[k] != 0)
                s1 = s1 + (char)ai1[k];
        }
        return s1;
    }

Thanks in advance

+5
source share
3 answers

If you are trying to parse a hexadecimal (base-16) number, use this:

int.Parse (tmp, NumberStyles.HexNumber);
+8
source

You need to convert the string to an integer, given that the string is in a specific database.

int i = Convert.ToInt32(str, 16);
int j = Convert.ToInt32("A", 16); // 10

So:

    for (int j = 0; j < i; j++)
    {
        int startIndex = j * 2;
        ai[j] = Convert.ToInt32(s.Substring(startIndex, 2));
    }
+5
source

Integer.valueOf(), s.substring() Java-, , :

ai[j] = Int32.Parse(s.Substring(j * 2, j * 2 + 2), 16);
+1

All Articles