First of all you should check the type of your value. You can do it by calling obj.GetType() method (either in your code directly or in Immediate window).

If it is int then you can do:

uint u = (uint) (int) obj;

Please note that it differs from your cast because it casts to int and then converts to uint while you were trying to cast to uint. int cannot be cast to uint and that is why you get the InvalidCastException. int can be only converted to uint. It is confusing that both conversion and cast operators look same in code: u = (uint) x.

Easier thing you can do is calling a specific method from Convert class:

uint u = Convert.ToUInt32(x);
Answer from Snowbear on Stack Overflow
🌐
Binary Convert
binaryconvert.com › convert_unsigned_int.html
Unsigned integer (32-bit) Converter
Online binary converter. Supports all types of variables, including single and double precision IEEE754 numbers
🌐
Simonv
simonv.fr › TypesConvert
Types converter
Independently of the input format, conversion is limited to 64 bits. ... Conversion in SINT8 type of the input value results in overflow. The displayed value is the result of the overflow. Note that overflow of signed 8-bits integer is undefined and therefore the result given here is for information only. ... Conversion in UINT8 ...
🌐
Cryptii
cryptii.com › pipes › integer-converter
Integer converter: Translate between 8, 16 and 32-bit ints - cryptii
In computer science, an integer is a data type that represents mathematical integers. They may be of different sizes and may or may not be allowed to contain negative values. In a computer they are commonly represented as a group of binary digits.
Top answer
1 of 8
241

Given:

 uint n = 3;

int i = checked((int)n); //throws OverflowException if n > Int32.MaxValue
int i = unchecked((int)n); //converts the bits only 
                           //i will be negative if n > Int32.MaxValue

int i = (int)n; //same behavior as unchecked

or

int i = Convert.ToInt32(n); //same behavior as checked

--EDIT

Included info as mentioned by Kenan E. K.

2 of 8
15

Assuming you want to simply lift the 32bits from one type and dump them as-is into the other type:

uint asUint = unchecked((uint)myInt);
int asInt = unchecked((int)myUint);

The destination type will blindly pick the 32 bits and reinterpret them.

Conversely if you're more interested in keeping the decimal/numerical values within the range of the destination type itself:

uint asUint = checked((uint)myInt);
int asInt = checked((int)myUint);

In this case, you'll get overflow exceptions if:

  • casting a negative int (eg: -1) to an uint
  • casting a positive uint between 2,147,483,648 and 4,294,967,295 to an int

In our case, we wanted the unchecked solution to preserve the 32bits as-is, so here are some examples:

Examples

int => uint

int....: 0000000000 (00-00-00-00)
asUint.: 0000000000 (00-00-00-00)
------------------------------
int....: 0000000001 (01-00-00-00)
asUint.: 0000000001 (01-00-00-00)
------------------------------
int....: -0000000001 (FF-FF-FF-FF)
asUint.: 4294967295 (FF-FF-FF-FF)
------------------------------
int....: 2147483647 (FF-FF-FF-7F)
asUint.: 2147483647 (FF-FF-FF-7F)
------------------------------
int....: -2147483648 (00-00-00-80)
asUint.: 2147483648 (00-00-00-80)

uint => int

uint...: 0000000000 (00-00-00-00)
asInt..: 0000000000 (00-00-00-00)
------------------------------
uint...: 0000000001 (01-00-00-00)
asInt..: 0000000001 (01-00-00-00)
------------------------------
uint...: 2147483647 (FF-FF-FF-7F)
asInt..: 2147483647 (FF-FF-FF-7F)
------------------------------
uint...: 4294967295 (FF-FF-FF-FF)
asInt..: -0000000001 (FF-FF-FF-FF)
------------------------------

Code

int[] testInts = { 0, 1, -1, int.MaxValue, int.MinValue };
uint[] testUints = { uint.MinValue, 1, uint.MaxValue / 2, uint.MaxValue };

foreach (var Int in testInts)
{
    uint asUint = unchecked((uint)Int);
    Console.WriteLine("int....: {0:D10} ({1})", Int, BitConverter.ToString(BitConverter.GetBytes(Int)));
    Console.WriteLine("asUint.: {0:D10} ({1})", asUint, BitConverter.ToString(BitConverter.GetBytes(asUint)));
    Console.WriteLine(new string('-',30));
}
Console.WriteLine(new string('=', 30));
foreach (var Uint in testUints)
{
    int asInt = unchecked((int)Uint);
    Console.WriteLine("uint...: {0:D10} ({1})", Uint, BitConverter.ToString(BitConverter.GetBytes(Uint)));
    Console.WriteLine("asInt..: {0:D10} ({1})", asInt, BitConverter.ToString(BitConverter.GetBytes(asInt)));
    Console.WriteLine(new string('-', 30));
}  
🌐
Online Tools
onlinetoolz.net › unsigned-signed
Convert between unsigned and signed - Online Tools
Enter an integer, consisting of x number of bits, and get it converted to its corresponding unsigned or signed value based on the rules of two's complement.
Find elsewhere
🌐
Uint256
uint256.net
uint256 converter | uint256
uint256 conversion/calculator - Using uint256 in solidity for ethereum
🌐
Embedded Wizard
doc.embedded-wizard.de › integer-conversion
Integer to integer - Type conversion
The resulting data type of the entire operation is consequently uint32 or uint64. For example: var int32 a = -1251; var uint32 b = 167; // 'a' is converted in 'uint32' and the addition // is performed with unsigned 32-bit precision.
🌐
Webmasters
number.webmasters.sk
Online Number Conversion
Portal | Tools | Measurement | Hangman | Nameday | Matches | BMI | Geography Exams | Diploma Thesis | Substitution Cipher | Morse Code | Vtipy a hlavolamy | Čítanie | Fotka | Kuchárka | Logo | Max hra | Funny Reading | Nákupné centrum | Športové centrum | Skratky | Spravodajstvo Mráčik | RSS katalóg | Instagram na SK i CZ | Twitter katalóg | Bankomaty v České republice | Bankomaty na Slovensku | Tvoj Lekár | Ponuky práce v zdravotníctve | Zdravotná poradňa | Tvůj lékař | Vyber školu | Kto hýbe Slovenskom | Kdo hýbe Českem | Palivové drevo | Tvoj Notár | Tvůj notář | Sudoku for Kids | Road for Kids | Pair for Kids | Hanoi for Kids | 15 for Kids | Grid for Kids | Colours for Kids | Pexeso | Logic | Einstein | Snake | 3 Wheels | Find 8
🌐
SCADACore
scadacore.com › home › tools › programming calculators
Online Hex Converter - Bytes, Ints, Floats, Significance, Endians - SCADACore
April 8, 2021 - Hex-To-UINT (Unsigned Integer) and Hex-To-INT (Singed Integer) Converts the Hex string to the 4 different Endian Combinations.
🌐
.NET Fiddle
dotnetfiddle.net › wPnD3j
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
🌐
Quora
quora.com › How-do-you-convert-a-signed-integer-to-an-unsigned-integer
How to convert a signed integer to an unsigned integer - Quora
Answer: As you cannot represent a negative number with unsigned integer, you can only do the “conversion” with a positive signed integer. When “converting” a positive signed integer to an unsigned integer, you will get the exact same value.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.convert.touint32
Convert.ToUInt32 Method (System) | Microsoft Learn
Converts the value of the specified single-precision floating-point number to an equivalent 32-bit unsigned integer. public: static System::UInt32 ToUInt32(float value);
🌐
AutoHotkey
autohotkey.com › board › topic › 49506-tip-convert-uint-to-int-and-other-conversion-tips
[Tip] Convert UInt to Int (and other conversion tips) - Ask for Help - AutoHotkey Community
9 hours ago - I found an obscure post from Sean who shared a trick for converting a UShort (unsigned 16-bit integer) to Short (signed integer) in one simple step: x:=x<<48>>48 ;-- Convert UShort to ShortNo if/then/else statements. No ternary operations. Just simple bit shifting. This conversion is valuable because a few of the older messages and WinAPI functions still return values in 16-bit variables and sometimes they need to be converted. Thanks Sean for the tip. The original post can be found here. What I need more often is a conversion from UInt (unsigned 32-bit integer) to Int (signed 32-bit integer).