The cast line as int causes a change in value

I am trying to read in a text file and change the value of a number on a specific line in Powershell on Windows 8.1. The file is somewhat predictable.

Currently, I do not find this the best way, but I'm trying to find out the cause of this error. I want to find the number in a specific place, and then read the integer value of this.

However, when I do this, I get the wrong value.

My code is:

$data = Get-Content "AssemblyInfo.cs"
Write-Output $data[-2]
Write-Output $data[-2][30]
Write-Output ([int]$data[-2][30])

Current output:

[assembly: AssemblyVersion("1.2.*")]
2
50

Expected Result:

[assembly: AssemblyVersion("1.2.*")]
2
2

Why does the value change from 2 to 50 when pressed as an int?

+4
source share
1 answer

, $data[-2] [String], [String] [String], [Char].

A [Char] ( ), . [int], , 64 @.

[String], [int] [int]::Parse():

[int][String]$data[-2][30]
# or
[int]::Parse($data[-2][30])
+5

All Articles