Search code examples
javac#inttype-conversionuint

Java to C# conversion - int uint types


I'm converting code from java to c# and got stuck with int and uint. Java code (excerpt):

public static final int SCALE_PROTO4_TBL    = 15;
public static final int [] sbc_proto_4 = { 0xec1f5e60 >> SCALE_PROTO4_TBL };

Converted c# code (excerpt):

public const int SCALE_PROTO4_TBL = 15;
int[] sbc_proto_4 = new int[] { 0xec1f5e60 >> SCALE_PROTO4_TBL };

That results in the following compile error: Error CS0266 Cannot implicitly convert type 'uint' to 'int'. An explicit conversion exists (are you missing a cast?)

There exist lots of code like the above but I'm not sure if I should convert all the Java int's to C# uint's ? As I recall C# uint doesn't store negative values to if the java int is a negative number then I got a problem.

Any input on how I should approach the problem ?


Solution

  • You can try like this:

    int[] sbc_proto_4 = new int[] { unchecked((int)0xec1f5e60) >> SCALE_PROTO4_TBL };
    

    ie, you have to cast the uint(0xec1f5e60) explicitly.