What is the equivalent of this snippet of Java code in C#?

Rika

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"
JWiley

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

need help translating a code snippet from java to C# equivalent

From Dev

need help translating a code snippet from java to C# equivalent

From Dev

Equivalent PyQt code for C++ Qt Snippet

From Dev

What is wrong with this snippet of C code?

From Dev

What is this Java code equivalent in C# ? (setRequestEntity)

From Dev

What is this Java code equivalent in C# ? (setRequestEntity)

From Dev

What exactly is happening in this Java code snippet?

From Dev

C++What is the meaning of "<" and ">" in this code snippet?

From Dev

What is the Java equivalent of this Scala code?

From Dev

How to convert Java code snippet into C#

From Dev

What is the function of this code snippet?

From Dev

What is the equivalent of the following tensorflow snippet in CNTK

From Dev

Meaning of a C code snippet

From Dev

What is the C++ equivalent to this MATLAB code?

From Dev

What is wrong with the following code snippet?

From Dev

What is wrong with this python code snippet?

From Dev

What is happening in this TypeScript code snippet?

From Dev

What is the space complexity of this code snippet

From Java

What is the equivalent of Java's final in C#?

From Dev

What is the equivalent of a C# gridview in java GUI?

From Dev

What is equivalent for logf from C in JAVA?

From Dev

What is c# equivalent of Java's getAndSet

From Dev

what is equivalent Swift code of this Objective-c code?

From Dev

How to simplify this code snippet in java?

From Dev

How to simplify this code snippet in java?

From Dev

What does this C snippet mean?

From Dev

C++ Code Snippet Execution

From Dev

Shared Memory Code Snippet in C

From Dev

What is the C# equivalent code for C programming statement?

Related Related

HotTag

Archive