Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Converting Words to ASCII Numbers and Back with PowerShell

$
0
0

Summary: Use Windows PowerShell to convert words to ASCII numbers and back again.

Microsoft Scripting Guy, Ed Wilson, is here. Today, I am sipping a nice cup of Earl Grey tea with just a little jasmine in it. Along with a cinnamon stick, it is a nice mellow cup. I am checking my email sent to scripter@microsoft.com on my Surface Pro 3, and playing around with Windows PowerShell. I thought it would be fun to open the project I started yesterday when I created a couple of hash tables.

Note  Today’s post continues yesterday's post: Automatically Create Hash Tables and Reverse Value with PowerShell. You really need to read yesterday’s post first, or today's post will not make any sense. (This does not mean it will make sense, but to have a fighting chance, you really need to read yesterday’s post.)

Put the hash tables to work

So I have two hash tables. The first hash table contains ASCII values and associated letters:

Image of hash table

Notice that I do not use an OrderedDictionary object because I could not do the look up like I want to if I use it. So the output of the hash table is a bit jumbled. That is fine, because I am not displaying it, only searching it.

The second hash table (the $ltrFirst hash table) is shown here:

Image of hash table

The first thing to do is to encode a string into an array of numbers. To do this, I store my string in a variable, and I call the ToCharArray method. I then pipe the characters to the Foreach-Object cmdlet (% is an alias), and I use the letters from the word to perform a look up in the $ltrFirst hash table. The result outputs an array of numbers equal to the ASCII values of the word:

PS C:\> $w = "dog"                                                               

$w.ToCharArray() | % {$ltrFirst["$_"]}

100

111

103

  Note  Because I am looking up against strings, I need to use quotation marks around the $_, which contains
  each letter from the word stored in $w.

To be useful, I need to store the array of numbers in a variable, as shown here:

$n = $w.ToCharArray() | % {$ltrFirst["$_"]}   

Decode

I want to be able to decode the numbers and convert them back into a word. To do this, I use the $ASCIIFirst hash table so that I can look up letters by their numeric value. I pipe the numeric array stored in the $n variable to the Foreach-Object cmdlet (% is the alias), and I use the number to look up by key in the $ASCIIFirst hash table. This time, I do not need any quotation marks because I am comparing numbers to numbers. Here is the example of doing this:

PS C:\> $n | % {$ASCIIFirst[$_]} 

d

o

g

So I know that my technique works. Now I store results in a variable instead of simply displaying them to the screen. This is shown here:

$c = $n | % {$ASCIIFirst[$_]}    

If I look at $c, I will still see an array of letters, not an actual word. To put the pieces back together, I use the –Join operator:

PS C:\> $c -join '' 

dog

Of course if I need to, I can store the results in another variable that I can use for other purposes. The complete code is shown here:

#AsciiEncodeDecodeWords.ps1

# ed wilson

# hsg-10-6,7-14

# -----------------------------------------------------------------------------

$ASCIIFirst=$ltrFirst=$null

$ASCIIFirst = @{}

$ltrFirst = @{}

97..122 |

  Foreach-Object {

    $ASCIIFirst.Add($_,([char]$_).ToString())}

 

foreach($k in $ASCIIFirst.Keys)

    {$ltrFirst.add($ASCIIFirst[$k],$k)}

 

$w = "dog"                                                               

$w.ToCharArray() | % {$ltrFirst["$_"]}                                   

$n = $w.ToCharArray() | % {$ltrFirst["$_"]}                              

$n | % {$ASCIIFirst[$_]}                                                 

$c = $n | % {$ASCIIFirst[$_]}                                            

$c -join ''            

That is all there is to using Windows PowerShell to use hash tables to convert words to ASCII characters and back. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>