1

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 ?

user1005448
  • 617
  • 3
  • 12
  • 22
  • you need to explicitly cast uint to int. But considering that may lead to a data loss. – Jean Nov 12 '15 at 09:38

1 Answers1

1

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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I'm aware of the unchecked keyword but I can't use that. E.g. if you print out the value of 0xec1f5e60 you'll see 3961478752. If you convert it using the unchecked keyword then the value will be -333488544 – user1005448 Nov 12 '15 at 09:44
  • Guess I should just a long instead of an int – user1005448 Nov 12 '15 at 09:47
  • @user1005448:- The point is you need to do an explicit cast of your uint value. I am not sure which value you want to use in your context. Hope you can now understand the error reason and how to resolve it – Rahul Tripathi Nov 12 '15 at 09:49
  • Can't use long as datatype since >> specifies that the second must be of type int – user1005448 Nov 12 '15 at 10:02
  • @user1005448:- What is the issue with `int[] sbc_proto_4 = new int[] { (int)0xec1f5e60 >> SCALE_PROTO4_TBL };` ? – Rahul Tripathi Nov 12 '15 at 10:03
  • it gives the following compile error: Error CS0221 Constant value '3961478752' cannot be converted to a 'int' (use 'unchecked' syntax to override) – user1005448 Nov 12 '15 at 10:06
  • @user1005448:- So now you got the point..right? ie, you have to use the `unchecked` keyword which I have added in my answer :) – Rahul Tripathi Nov 12 '15 at 10:07
  • Can't use unchecked 'cause 0xec1f5e60 will overflow and result in a negative number. – user1005448 Nov 12 '15 at 10:23
  • 1
    I tried to look at the hex value which is the same so I'm gonna accept your answer to use unchecked. Thanks for your time. – user1005448 Nov 12 '15 at 12:04