1

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?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459

1 Answers1

3

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.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • OK Already noticed that before but if i change num from uint to int i have the next error on num2. cannot convert from 'int' to 'uint' If i change num2 from uint num2 = metoda6(num, uint_0, uint_1); to int num2 = metoda6(num, uint_0, uint_1); i got a new error: The best overloaded method match for 'WindowsFormsApplication1.Form1.metoda6(uint, uint, uint)' has some invalid arguments Here is the whole project (C#) VS2010 http://www70.zippyshare.com/v/45354586/file.html – Gherghina Ionut-Valentin Jul 15 '13 at 08:51
  • You can always explicitly cast by using `(int)`. – Bart Friederichs Jul 15 '13 at 10:23