Error 1 Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?)

StackOverflow https://stackoverflow.com/questions/17649077

  •  03-06-2022
  •  | 
  •  

Question

I'm kinda new to C# coding and i have dealed lately with a problem and i want to ask for your help. I have this piece of code from the link below.

http://pastebin.com/5jQe5tQs

My problem is at the finish line:

uint num = ((string_0[0] | (string_0[1] << 8)) |
            (string_0[2] << 0x10)) | (string_0[3] << 0x18);

Where i got this error:

Cannot implicitly convert type 'int' to 'uint'. An explicit conversion exists (are you missing a cast?)

From what i know and read i see that the problem came from a value asigned for a int with is too big for the int values. But from my little experience i do not know from what variable the value is not in the right format. A more advised expert can help me please to fix the code?

No correct solution

OTHER TIPS

The result of the expression

((string_0[0] | (string_0[1] << 8)) | (string_0[2] << 0x10)) | (string_0[3] << 0x18)

is an int. You are putting it into a uint. Just make num an int, or cast the expression to uint.

What happens is, when you derefence a part of the string by using the [] operator, you fetch a char. Because shift operators for char do not exist, the value will be upgraded implicitly to int (upgrading is no problem, because there is no risk of loss of accuracy). So this part (as the others as well):

(string_0[1] << 8)

returns an int.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top