Reputation: 18227
I want to do something like this (I tried four different ways). Create a string value that contains hex characters:
$searchString = "filterValue\xEF\xBF\xBD"
#$searchString = "filterValue" + 0xEF -as [char] + 0xBF -as [char] + 0xBD -as [char]
#$searchString = "filterValue" + 0xEF + 0xBF + 0xBD
$searchString = "filterValue\0xEF\0xBF\0xBD"
Write-Host "$searchString len=$($searchString.Length)"
Length should be 14.
Is there someone to add them inline rather than creating three separate char values as shown in this post: Writing a hex escaped char in Powershell
I tried the longer way:
$char1 = (0xEF -as [char])
$char2 = (0xBF -as [char])
$char3 = (0xBD -as [char])
$searchString = "filterValue" + $char1 + $char2 + $char3
Write-Host "$searchString len=$($searchString.Length)"
$posSearchString = $filterBytes.IndexOf("$searchString")
But that doesn't seem to match what I'm looking for:
Upvotes: 1
Views: 2171
Reputation: 200523
Use subexpressions:
$searchString = "filterValue$([char]0xEF)$([char]0xBF)$([char]0xBD)"
String concatenation or using the format operator would work too as long as you cast the values:
$searchString = "filterValue" + [char]0xEF + [char]0xBF + [char]0xBD
$searchString = "filterValue{0}{1}{2}" -f [char]0xEF, [char]0xBF, [char]0xBD
If you want to use the -as
operator instead of casting the numbers to chars you need to group the expressions:
$searchString = "filterValue" + (0xEF -as [char]) + (0xBF -as [char]) + (0xBD -as [char])
$searchString = "filterValue{0}{1}{2}" -f (0xEF -as [char]), (0xBF -as [char]), (0xBD -as [char])
Upvotes: 2