01010000 01101111 01110111 01100101 01110010 01010011 01101000 01100101 01101100 01101100 00100000 01010110 00110011 00100000 01010010 01101111 01100011 01101011 01110011 00100001
So what did I type here? Well, you can either copy this text and go find a binary-to-text converter online… Or you can use the Fun Text Converter that I wrote in PowerShell using ShowUI.
First thing is first, you need to download ShowUI and load up the module. You can download the latest version (1.3 at the time of this writing) from here. Once you have it downloaded and copied to your module folder, you can then import the module using the following command:
Import-Module ShowUI
Ok, now we are ready to go.
First thing I do is set up the window by using a hash table that will hold the parameters for the Window.
$Windowparam = @{ Width = 500 Height = 400 Title = 'Fun Text Converter' Background = '#C4CBD8' WindowStartupLocation = 'CenterScreen' AsJob = $True }
For those of you unfamiliar with this technique, it is called “Splatting” and it is very useful and looks cleaner when used in a command (as you will see soon).
Ok, lets take a look at our window with the given parameters:
New-Window @Windowparam -asjob
Ok, nothing too spectacular here, but you can see that the Window is being displayed just as I configured it to using splatting. All you have to do is run the command and add the hash table, in this case @Windowparam. Note that you do not use $windowparam.
Now lets add the rest of the components needed to make this UI work.
#Create Window New-Window @Windowparam { New-Grid -Rows *,Auto,*,Auto -Children { New-TextBox -Row 0 -Name InputBox -TextWrapping Wrap -VerticalScrollBarVisibility Auto New-Grid -Row 1 -Columns *,*,Auto,Auto,Auto,*,* -Children { New-Button -Column 2 -Name ConvertButton -Width 65 -Height 25 -Content Translate -On_Click { If ($ComboBox.Text -eq 'TextToBinary') { $OutputBox.Text = Convert-TextToBinary $InputBox.Text } ElseIf ($ComboBox.Text -eq 'BinaryToText') { $OutputBox.Text = Convert-BinaryToText $InputBox.Text } ElseIf ($ComboBox.Text -eq 'TextToHex') { $OutputBox.Text = Convert-TextToHex $InputBox.Text } ElseIf ($ComboBox.Text -eq 'HexToText') { $OutputBox.Text = Convert-HexToText $InputBox.Text } ElseIf ($ComboBox.Text -eq 'BinaryToHex') { $OutputBox.Text = Convert-BinaryToHex $InputBox.Text } ElseIf ($ComboBox.Text -eq 'HexToBinary') { $OutputBox.Text = Convert-HexToBinary $InputBox.Text } ElseIf ($ComboBox.Text -eq 'ReverseInput') { $OutputBox.Text = Convert-TextToReverseText $InputBox.Text } } New-Label -Column 3 New-ComboBox -Name ComboBox -Column 4 -IsReadOnly:$True -SelectedIndex 0 -Items { New-TextBlock -Text TextToBinary New-TextBlock -Text BinaryToText New-TextBlock -Text TextToHex New-TextBlock -Text HexToText New-TextBlock -Text BinaryToHex New-TextBlock -Text HexToBinary New-TextBlock -Text ReverseInput } } New-TextBox -Row 2 -Name OutputBox -IsReadOnly:$True -TextWrapping Wrap ` -VerticalScrollBarVisibility Auto New-StackPanel -Row 3 -Orientation Horizontal { New-Button -Name CopyTextButton -Width 65 -Height 25 -HorizontalAlignment Left -Content CopyText -On_Click { $OutputBox.text | clip } New-Label New-Button -Name ClearTextButton -Width 65 -Height 25 -HorizontalAlignment Left -Content ClearText -On_Click { $OutputBox.Text=$Null } } } }
Ok, now I have a nice UI that includes 3 buttons, 2 textboxes and a combo box. By not supplying a height and width for each of the textboxes, they will automatically adjust their size when you re-size the window.
Now that I have the front-end stuff done, it is time to knock out the backend things to make this converter work like I want it to. You may have noticed the functions that were being called when you click on the Translate button. Well, that is the next part of this little piece. Since I am running the UI as a job, I opted to include the functions when the Window is loaded. This is done by adding the –On_Loaded parameter.
I decided that I wanted to accomplish the following types of conversions in this UI. They include:
- TextToBinary
- BinaryToText
- TextToHex
- HexToText
- BinaryToHex
- HexToBinary
- ReverseInput
Using the following functions, I was able to accomplish each of these items:
Edit: Changed [string]::Join() to –Join. Thanks Robert (Twitter | Blog) for keeping me honest!
Function Convert-TextToBinary { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$Text ) Begin { #Create binary empty collection [string[]]$BinaryArray = @() } Process { #Convert text to array $textarray = $text.ToCharArray() #Convert each item to binary ForEach ($a in $textarray) { $BinaryArray += ([convert]::ToString([int][char]$a,2)).PadLeft(8,"0") } } End { #Write out binary string $BinaryArray -Join " " } } Function Convert-BinaryToText { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$Binary ) Begin { #Create binary empty collection [string[]]$TextArray = @() } Process { #Split Binary string into array $BinaryArray = $Binary -split "\s" #Convert each item to Char ForEach ($a in $BinaryArray) { $TextArray += [char]([convert]::ToInt64($a,2)) } } End { #Write out text string $TextArray -Join "" } } Function Convert-TextToHex { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$Text ) Begin { #Create hex empty collection [string[]]$HexArray = @() } Process { #Convert text to array $textarr = $text.ToCharArray() #Convert each item to binary ForEach ($a in $textarr) { $HexArray += "0x$(([convert]::ToString([int][char]$a,16)).PadLeft(8,'0'))" } } End { #Write out hex string $HexArray -Join " " } } Function Convert-HexToText { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$Hex ) Begin { #Create text empty collection [string[]]$textarr = @() } Process { #Split Binary string into array $HexArray = $Hex -split "\s" #Convert each item to Char ForEach ($a in $HexArray) { $textarr += [char]([convert]::ToInt64($a.TrimStart('x0'),16)) } } End { #Write out text string $textarr -join "" } } Function Convert-HexToBinary { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$Hex ) Begin { #Create binary empty collection [string[]]$binarr = @() } Process { #Split Binary string into array $HexArray = $Hex -split "\s" #Convert each item to Char ForEach ($a in $HexArray) { $a = ([char]([convert]::ToInt64($a.TrimStart('x0'),16))) $binarr += ([convert]::ToString([int][char]$a,2)).PadLeft(8,"0") } } End { #Write out binary string $binarr -join " " } } Function Convert-BinaryToHex { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$Binary ) Begin { #Create binary empty collection [string[]]$TextArray = @() } Process { #Split Binary string into array $BinaryArray = $Binary -split "\s" #Convert each item to Char ForEach ($a in $BinaryArray) { $a = [char]([convert]::ToInt64($a,2)) $TextArray += "0x$(([convert]::ToString([int][char]$a,16)).PadLeft(8,'0'))" } } End { #Write out hex string $TextArray -Join " " } } Function Convert-TextToReverseText { [cmdletbinding()] Param ( [parameter(ValueFromPipeLine='True')] [string]$InputString ) Begin { } Process { #Convert text to array $inputarray = $InputString -split "" #Reverse array [array]::Reverse($inputarray) } End { #Write out reverse string $inputarray -join "" } }
Putting this altogether gives us a fully functional text converter.
.\ShowUI_TextConverter.ps1
So… Lets find out what it was that I posted earlier on in this article:
That is right….PowerShell V3 Rocks!
Using the Fun Text Converter
Using the converter is pretty simple. Type or paste text or whatever into the top window and then use the combobox in the middle right of the UI to select the type of conversion to use and the click on Translate to start the conversion. The output of the converted is displayed in the bottom window. This window is read-only and you can copy the text by highlighting it….or you can use the CopyText button to copy it for you. Clicking on ClearText will only clear the output window when clicked.
Let’s see a couple more conversions:
Download Fun Text Converter
(remove .doc extension)
Fabulous!
This is the first Powershell ShowUI script I found that actually does something.
The problem is that when I tried it out in the lastest ShowUI on my Win7 Powershell, it doesn’t work any more.
Could you update the script to the lastest ShowUI please?
Thanks
hi smetah,
I am new to Powershell and showUI but this seems to help solving the problem
I just removed the functions declared in -on_Loaded and copied them in separate files…
dot source them all
. .\Convert-TextToBinary.ps1
. .\Convert-BinaryToText.ps1
. .\Convert-TextToHex.ps1
. .\Convert-HexToText.ps1
. .\Convert-HexToBinary.ps1
. .\Convert-BinaryToHex.ps1
. .\Convert-TextToReverseText.ps1
Have to use Show-UI instead of Window @Windowparam
Need some window formatting but everything else seems to work!