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