Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell bizarre variable process

I've been banging my head, about this ...

Created this simple PowerShell script :

test.ps1:

       Write-Host    $args[0]
       Write-Host  $($args[0])
       Write-Host "$($args[0])"

Now, run the script twice, with these parameters :

      powershell .\test.ps1 0E2C
      powershell .\test.ps1 0E2D

The first run returns :

      0E2C
      0E2C
      0E2C

The second run returns :

      0E2D
      0
      0

Why is that ? Why is powershell converting 0E2D to 0 ? Is it considering that 0E2D is an hexadecimal value and trying to convert it somehow ? But why, and why doesn't it do the same with value 0E2C ?

EDIT :Thank you all.

I solved the issue by using a variable, and forcing it to string So, changing my TEST.PS1 :

Write-Host    $args[0]
Write-Host  $($args[0])
Write-Host "$($args[0])"
[string] $myvar = $args[0]
Write-Host "$myvar"

Now, when I run :

      powershell .\test.ps1 0E2D

I now get :

      0E2D
      0
      0
      0E2D
like image 479
José Pinho Avatar asked Aug 02 '26 07:08

José Pinho


1 Answers

Powershell interprets 0E2D as a Decimal 0E2 which is 0*10^2 which is 0
The d suffix is documented here:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_numeric_literals?view=powershell-7.2#real-literals

like image 122
yxc Avatar answered Aug 05 '26 19:08

yxc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!