Martin Fric
Martin Fric

Reputation: 740

Powershell: UTF-8 value of special character

I need to replace char:

Ť

with something like this:

\u0164

I have following simple try

function ReplaceNonISOChar($val) {
    $regex = [regex] $('[^\u0000-\u00ff]')
    $res = ""
    foreach ($char in [char[]]$val) {
        $utf = '{0:d4}' -f [int][char]$char + "" 
        $res += $char -replace $regex, "\u$utf"
    }
    return $res
}
$result = ReplaceNonISOChar -val 'Ť'
Write-Host $result

But it returns me integer value (not unicode). If regex can hit the pattern and find this char, it should be possible within powershell retrieve also this value backwards.

Can anybody give me some hints?

Thanks

Upvotes: 1

Views: 818

Answers (1)

Janne Tuukkanen
Janne Tuukkanen

Reputation: 1660

Insted of decimal (d) you should tell the script to use hexadecimal (x) notation:

$utf = '{0:x4}' -f [int][char]$char + ""

Upvotes: 1

Related Questions