To convert your array of characters to lowercase you can use String.ToLower
method:
$letters=@('A','B','C')
$arr = foreach ($x in $letters) {
$l.toLower()
}
Or even shorter (thx TheIncorrigible1 for mentioning member enumeration in your comment):
$letters=@('A','B','C')
$letters.ToLower()
You can read more about member enumeration in this answer.
If you really want to convert to operate on ASCII values, you have to convert to char and then to int like so:
$letters = @('A','B','C')
$arr = foreach ($l in $letters) {
$converted = [int][char]$l
# Check if uppercase
if ($converted -ge 65 -and $converted -le 90) {
# Convert to lowercase by adding 32
$converted += 32
}
# Return
$converted
}
$convertedToLower=[char[]]$arr
Write-Host $convertedToLower
Few tips to your code:
- use meaningful variable naming - it makes code easier to understand
- if using multiple conversions, make sure they work. For example start with:
$x = $t[0]
[int]$x
and you'll receive an error immediately.
- By using
$y = 0
you specify $y
to be int
. When you add any numeric value to int, it'll sum it instead creating array. If you want an array, initialize it with $y = @()
.
- You tried to compare
$y
with 122 while $y
should be (that's just my guess as it's unclear from your code) array. Comparing array to integer doesn't sound like good idea so try to avoid it as it might give you surprising results.