1

I am trying to convert a calculated value to uint16. I've hard coded for the sake of the example. In C# it works. But, what I believe is the same code in Powershell fails. Consider the following:

In the powershell code example it produces:

Cannot convert value "101398986" to type "System.UInt16". Error: "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [uint16]$g
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastIConvertible

I've tried [convert]::ToUInt16($g) it produces:

Exception calling "ToUInt16" with "1" argument(s): "Value was either too large or too small for a UInt16."
At D:\OneDrive\Desktop\VaribleCasting2.ps1:2 char:1
+ [uint16]$v = [convert]::ToUInt16($g)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : OverflowException

PowerShell(fails):

$g = 101398986
[uint16]$v = [uint16]$g
$v

C#(succeeds):

using System;

namespace NumericTypeTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            var g = 101398986;
            UInt16 v = (UInt16)g;
            Console.WriteLine(v);
            Console.ReadLine();
        }
    }
}

My expectation was that .NET in both platforms would produce the same result. Thanks for any help!

Todd
  • 119
  • 10
  • 2
    i don't think you are getting a real UInt16 in c#. take a look at this >>> `[uint16]::MaxValue` <<< note that the max value is 64k ... which is _exactly_ what one would expect. your number is 101k+ ... [*grin*] – Lee_Dailey May 06 '19 at 22:21
  • https://learn.microsoft.com/en-us/dotnet/api/system.uint16.maxvalue – Riley Carney May 06 '19 at 22:51
  • Thank you, I am aware of the limits. What I am asking is why does this work in C# .NET but not PowerShell .NET. Clearly there is something going on here I don't understand. This is clearly not the same cast. – Todd May 06 '19 at 23:35
  • take a look at the actual result in c# - i will bet you that the number is either _truncated_ or coerced into a larger `[int]` type. [*grin*] – Lee_Dailey May 06 '19 at 23:42

2 Answers2

5

Powershell does checked arithmetic by default (like Visual Basic), unlike C#. This means you do get OverflowException by default when you overflow, instead of the runtime silently truncating the result.

You might want to look at How to suppress overflow-checking in PowerShell? for more details.

mostanes
  • 149
  • 6
0

You are trying to give your 16 bits integer the number 101398986, that's totally impossible as long a your are using 16 bits, cause a 16 bit integer need to be under or equal 65,535

Or if its signed integer : -32,767 < number < 32,767

So better to use an 32 bits integer for your case:

var g = 101398986;
UInt32 v = (UInt32)g;

Where your number must be under or equal 4294967295 (for 32 bits integer)

Mobrine Hayde
  • 365
  • 1
  • 2
  • 10