-2

Is it possible to convert an integer into an unsinged integer without the need of explicit typecasts?

I have the following code -

using System;

class MainClass
{
    public static void Main ()
    {
        int num1 = -33;
        uint num2 = Convert.ToUInt32 (num1);
        Console.WriteLine (num2);
    }
}

It throws me the error -

System.OverflowException has been thrown.

Value was either too large or too small for a UInt32

I don't want to use explicit typecasting. Is it possible this way?

The question is not a duplicate of the question How can I convert a signed integer into an unsigned integer? As I have specifically mentioned that I want to know is it possible without using explicit typecasting or not?

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45
Jonita Gandhi
  • 41
  • 2
  • 7
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/162160/discussion-on-question-by-jonita-gandhi-in-c-is-it-possible-to-use-convert-toui). – Bhargav Rao Dec 29 '17 at 12:23

2 Answers2

3

I want to know if it is possible without explicit typecasting?

The short answer is: Nope.

I want to know the reason of existence of the function Convert.ToUInt32()

Answer: Someone at microsoft found it helpful and created that. Others found it helpful/easy to use and used it. I guess we won't find the concrete reason for implementing that function.

Further information on this topic:

This answer states 4 possibilities. Each of them include a typecast. I don't know another built-in feature to convert uint to int. That answer nicely explains the converting of uint->int and it's pitfalls.

(UltraShort)Summary: You have to take care of overflows.

You should have a look at checked and unchecked keywords when dealing with unsigned types.

TypeConversion

Depending on this there are 4 methods of TypeConversion.

  • Implicit
  • Explicit
  • User-defined conversions using conversion oerators.
  • Conversions with helüer classes (Like the Convert class)

The usecase of Convert.ToInt32() / Convert.ToUInt32()

Codesample for ToInt32() is taken from msdn.

float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f,
                  0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue };
int result;

foreach (float value in values)
{
   try {
      result = Convert.ToInt32(value);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        value.GetType().Name, value, result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Int32 type.", value);
   }   
}                                 
// The example displays the following output:
//    -3.40282346638529E+38 is outside the range of the Int32 type.
//    -13799999488 is outside the range of the Int32 type.
//    Converted the Double value -1023.29901123047 to the Int32 value -1023.
//    Converted the Double value -12.9799995422363 to the Int32 value -13.
//    Converted the Double value 0 to the Int32 value 0.
//    Converted the Double value 9.11299983940444E-16 to the Int32 value 0.
//    Converted the Double value 103.918998718262 to the Int32 value 104.
//    Converted the Double value 17834.19140625 to the Int32 value 17834.
//    3.40282346638529E+38 is outside the range of the Int32 type.

Codesample for Convert.ToUInt(Int32) also taken from msdn

int[] numbers = { Int32.MinValue, -1203, 0, 121, 1340, Int32.MaxValue };
uint result;
foreach (int number in numbers)
{
   try {
      result = Convert.ToUInt32(number);
      Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                        number.GetType().Name, number,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.",
                        number.GetType().Name, number);
   }
}
// The example displays the following output:
//    The Int32 value -2147483648 is outside the range of the UInt32 type.
//    The Int32 value -1203 is outside the range of the UInt32 type.
//    Converted the Int32 value 0 to the UInt32 value 0.
//    Converted the Int32 value 121 to the UInt32 value 121.
//    Converted the Int32 value 1340 to the UInt32 value 1340.
//    Converted the Int32 value 2147483647 to the UInt32 value 2147483647.
Tobias Theel
  • 3,088
  • 2
  • 25
  • 45
1

As request by the OP comment:

//Sample inputs
string str = "15";
bool b = false;
int i = 15;
double d = 14.5;
object o = str;
object oi = i;

//These are not valid
//uint ustr = (uint)str; 
//uint ub = (uint)b;

//This is valid at compile time but throw a run time exception
//uint uo = (uint)o;
//uint uoi = (uint)oi;

//These are valid
uint ustr = Convert.ToUInt32(str);
uint ub = Convert.ToUInt32(b);
uint uo = Convert.ToUInt32(o);
uint uoi2 = Convert.ToUInt32(oi);

//both ways are valid
uint ud = (uint)d;
uint ud2 = Convert.ToUInt32(d);
uint ui = (uint)i;
uint ui2 = Convert.ToUInt32(i);

//Some inputs can't be converted by either way (casting may be possible but result in wrong values)
string str2 = "Bananas";
int i2 = -15;
Magnetron
  • 7,495
  • 1
  • 25
  • 41