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 ?

1
  • you need to explicitly cast uint to int. But considering that may lead to a data loss. Commented Nov 12, 2015 at 9:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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
Guess I should just a long instead of an int
@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
Can't use long as datatype since >> specifies that the second must be of type int
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.