2

I stumbled upon this website and I am trying to test the idea, I don't know Java so I tried converting it to C#. Everything seems trivial but I get some exception while executing the sample.

I guess there must be something I am doing wrong here. The method which generates that exception is below :

 static void setSolution(String newSolution)
 {
        solution = new byte[newSolution.length()];
        // Loop through each character of our string and save it in our byte
        // array
        for (int i = 0; i < newSolution.length(); i++){
            String character = newSolution.substring(i, i + 1);
            if (character.contains("0") || character.contains("1")){
                solution[i] = Byte.parseByte(character);
            } else {
                solution[i] = 0;
            }
        }
    }

And this is my C# based method:

public static void SetSolution(string newSolution)
{
    solution = new byte[newSolution.Length];
    // Loop through each character of our string and save it in our byte
    // array
    for (int i = 0; i < newSolution.Length; i++)
    {
        string character = newSolution.Substring(i, i + 1);
        if (character.Contains("0") || character.Contains("1"))
        {
            solution[i] = Byte.Parse(character);
        }
        else
        {
            solution[i] = 0;
        }
    }
}

Am I converting it correctly? since it doesn't make sense to convert, for example, 1000 to byte! as it is static the string retains its old values and thus in the 4th iteration it spits out an OverFlow Exception:

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

Additional information: Value was either too large or too small for an unsigned byte.

I also tried

solution[i] =  Convert.ToByte(newSolution[i]);

which again doesn't seem to be it.

Edit

This is the input string:

 "1111000000000000000000000000000000000000000000000000000000001111"
halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224
  • Have you tried just implicitly converting it? `solution[i] = (byte) character;`? – AdamMc331 Oct 15 '14 at 15:20
  • I also don't understand, you're trying to set it to 0 if it contains 0, and 1 if it contains 1, and then 0 if it doesn't? Why can't you just change it to: if it contains 1, make it 1. Otherwise, make it 0. – AdamMc331 Oct 15 '14 at 15:21
  • What is your input string? – durron597 Oct 15 '14 at 15:21
  • Are you sure the Java version works? `i+1` when `i == newSolution.length - 1` is out of bounds. – Elliott Frisch Oct 15 '14 at 15:22
  • Compare the substring functions between java and C#, they don't convert over 1 for 1. – JWiley Oct 15 '14 at 15:23
  • Since `character` will always have length 1, using `contains` in either language seems a bit heavy; also, you could just extract the first character (using `charAt` in Java) and subtract `'0'` from it (this has the value 48), instead of trying to find a parsing method. – ajb Oct 15 '14 at 15:24
  • I have zero knowledge of java, thats why i am trying to convert it to c#. I couldnt run it, so it thought making it a c# sample and testing it would be a better Idea! – Hossein Oct 15 '14 at 15:25
  • I think this does exactly what the java code does: var bytes = newSolution.Select(c => (c == '0' || c == '1') ? byte.Parse(c.ToString()) : (byte)0).ToArray(); – wilford Oct 15 '14 at 15:31
  • You are right, I will remove my comment! – JWiley Oct 15 '14 at 15:37

2 Answers2

6

The substring functions are not the same between Java and C#:

Java:

public String substring(int beginIndex, int endIndex)

C#:

public string Substring(int startIndex, int length)

Convert this line to reflect what it's doing in Java.

The equivalent would be:

public static void SetSolution(string newSolution)
{
    solution = new sbyte[newSolution.Length];
    // Loop through each character of our string and save it in our byte
    // array
    for (int i = 0; i < newSolution.Length; i++)
    {
        string character = newSolution.Substring(i, 1); 
        if (character.Contains("0") || character.Contains("1"))
        {
            solution[i] = SByte.Parse(character);
        }
        else
        {
            solution[i] = 0;
        }
    }
}
JWiley
  • 3,129
  • 8
  • 41
  • 66
  • 1
    Good answer, however I'll nitpick: the Java 'byte' is signed, so the C# equivalent would be 'sbyte' and 'SByte.Parse'. – Dave Doknjas Oct 15 '14 at 17:38
  • Good catch- I was simply looking for the root of the exception he was seeing, I'll update the answer. – JWiley Oct 15 '14 at 17:39
5

As the question stands how to convert to C#, I would personally use Linq:

public static void SetSolution(string newSolution)
{
    solution = newSolution.Select(c => c == '1' ? (byte)1 : (byte)0).ToArray();
}

I believe the above code should be functionally equivalent to your java snippet as basically all the characters except for '1' are converted to 0.

Michal Hosala
  • 5,570
  • 1
  • 22
  • 49