August 21st, 2006

Fun with PowerShell – character manipulation…

PowerShell Team
PowerShell Team

I was investigating a localization test failure today and ran into the following error message:

 

‘actual error is 爠捡湩敤氠捥整牵꿿渠攧楸瑳畯渠攧瑳瀠獡甠獳敩⹲਍, expected ‘

 

Since it was failing in the French locale, getting what appeared to be a Chinese error message didn’t make a lot of sense. A coworker confirmed that it was garbage. My next guess was that it was actually an ANSI string that was mangled into Unicode. In other words, two characters in the source string became one character in the output. But how to test this? Well – fortunately we have PowerShell! I pasted the string into the console window which looked like:

 

$t = ‘??????????????????????????????????’

 

And then used casts to take the string apart:

 

&{$ofs=”; [string][char[]] ([int[]] [char[]] $t |% { $_ -band 0xff ;  [int] ($_ / 256 ) })}

 

Running this exercise in stunt-casting translated $t back into:

 

La racine de lecteur rÿF:\ÿ° n’existe pas ou n’est pas un dossier.

 

Now that’s more like the error message one would expect in the French locale J

 

Anyone want to take a stab at explaining how this works?

 

-bruce

 

Bruce Payette [MSFT]
Windows PowerShell Tech Lead

Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.