Skip to content

PowerShell doesn't understand unsigned numbers #3313

@viorel-m

Description

@viorel-m

Steps to reproduce

Description: I can't make powershell use correctly types like [uint32] or [uint64]. It seems like it always tries to dumb down every number to [int], and does it poorly. The bug seems to be that PS doesn't realize that is has to upcast 0xffffffff to int64 and not to int32, as it won't fit.

PS C:> [uint32]$b=0xffffffff
Cannot convert value "-1" to type "System.UInt32". Error: "Value was either too large or too small for a UInt32."

PS C:> $b=[uint32]0xffffffff
Cannot convert value "-1" to type "System.UInt32". Error: "Value was either too large or too small for a UInt32."

PS C:> $b=[uint64]0x1ffffffff
<so this works, because powershell realizes that 1ffffffff doesn't fit into 31 bit>

PS C:> $b=[uint64]0xffffffff
Cannot convert value "-1" to type "System.UInt64". Error: "Value was either too large or too small for a UInt64."

Expected behavior

PS C:\ > [uint32]$b = 0xffffffff
PS C:\ > $b
4294967295

Actual behavior

PS C:> [uint32]$b = 0xffffffff
Cannot convert value "-1" to type "System.UInt32". Error: "Value was either too large or too small for a UInt32."
At line:1 char:1

  • [uint32]$b = 0xffffffff
  •   + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
      + FullyQualifiedErrorId : RuntimeException
    
    
    

Environment data

> $PSVersionTable
PS C:\> $PSVersionTable

Name                           Value                                                                                                                                                      
----                           -----                                                                                                                                                      
PSVersion                      5.1.15050.0                                                                                                                                                
PSEdition                      Desktop                                                                                                                                                    
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                    
BuildVersion                   10.0.15050.0                                                                                                                                               
CLRVersion                     4.0.30319.42000                                                                                                                                            
WSManStackVersion              3.0                                                                                                                                                        
PSRemotingProtocolVersion      2.3                                                                                                                                                        
SerializationVersion           1.1.0.1                                                                                                                                                    

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.WG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions