You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Powershell中定义128位及更大长度的无符号整数变量?

PowerShell 7.2中使用128位/256位无符号整数的解决方案

PowerShell 7.2 基于.NET 6 运行时,原生不内置 UInt128、UInt256 类型,无法直接声明这两个类型的变量,可通过两种方案满足需求:

  • 方案1:升级PowerShell到7.3及以上版本
    PowerShell 7.3 及后续版本基于.NET 7+ 运行时,微软官方在.NET 7 新增了System.UInt128、System.UInt256原生类型,可直接声明使用:
    # 声明UInt128变量
    [System.UInt128]$uint128Val = 340282366920938463463374607431768211455
    # 声明UInt256变量
    [System.UInt256]$uint256Val = [System.UInt256]::MaxValue
    
  • 方案2:保留PowerShell 7.2版本,使用System.Numerics.BigInteger替代
    .NET 6 已内置System.Numerics.BigInteger结构,支持任意精度的整数运算,完全可以满足128位、256位无符号整数的存储、计算需求:
    # 导入命名空间(可选,简化调用)
    using namespace System.Numerics
    
    # 存储128位无符号整数最大值
    [BigInteger]$my128BitUint = [BigInteger]::Parse("340282366920938463463374607431768211455")
    # 存储256位无符号整数最大值
    [BigInteger]$my256BitUint = [BigInteger]::Parse("115792089237316195423570985008687907853269984665640564039457584007913129639935")
    
    该方案优势是无需升级运行环境,可直接兼容现有PowerShell 7.2版本;劣势是BigInteger为可变长度类型,没有固定长度类型的内存对齐、原生接口互操作等特性,仅做数值存储、运算完全够用。

内容的提问来源于stack exchange,提问作者martorad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 05:06:03