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!