How to use System.Convert.Int32() to convert ushort to int?
Hi,
I'm trying to convert a ushort into an int. I've tried explicitly casting it and that has not worked for me, so I tried using System.Convert.Int32(), and that also has not worked. Both of these have only returned the original ushort. For instance:
ushort vIn = 33;
int vOut = Convert.ToInt32(vIn);
Debug.Log (vOut);
I'm not sure what I'm doing wrong, any help will be greatly appreciated!
Answer by Backside2357 · Jan 04, 2018 at 07:30 PM
What do you mean when you say "has not worked"? And what result do you expect? Because in your example, vIn
has a positive integer value of 33. If the cast works, the expected result is that vOut
also contains the value 33 and that you don't get a compiler error.
Answer by MaxGuernseyIII · Jan 04, 2018 at 07:29 PM
ushort should be able to explicitly cast to int.
var myUshort = ushort.MaxValue;
var i = (int)myUshort;
Could you add information about how it's not working for you? What do you mean by "have only returned the original ushort"? What other result would you expect but the original value typed as a 32-bit, signed integer?