{"id":577,"date":"2014-10-07T00:01:00","date_gmt":"2014-10-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/10\/07\/converting-words-to-ascii-numbers-and-back-with-powershell\/"},"modified":"2014-10-07T00:01:00","modified_gmt":"2014-10-07T00:01:00","slug":"converting-words-to-ascii-numbers-and-back-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/converting-words-to-ascii-numbers-and-back-with-powershell\/","title":{"rendered":"Converting Words to ASCII Numbers and Back with PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Use Windows PowerShell to convert words to ASCII numbers and back again.<\/span><\/p>\n<p>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 <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> on my Surface Pro&nbsp;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.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> Today&rsquo;s post continues yesterday&#039;s post: <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/automatically-create-hash-tables-and-reverse-values\/\" target=\"_blank\">Automatically Create Hash Tables and Reverse Value with PowerShell<\/a><i>.<\/i> You really need to read yesterday&rsquo;s post first, or today&#039;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&rsquo;s post.)<\/p>\n<h2>Put the hash tables to work<\/h2>\n<p>So I have two hash tables. The first hash table contains ASCII values and associated letters:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-7-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-7-14-01.png\" alt=\"Image of hash table\" title=\"Image of hash table\" \/><\/a><\/p>\n<p>Notice that I do not use an <b>OrderedDictionary<\/b> 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.<\/p>\n<p>The second hash table (the <b>$ltrFirst<\/b> hash table) is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-7-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-10-7-14-02.png\" alt=\"Image of hash table\" title=\"Image of hash table\" \/><\/a><\/p>\n<p>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 <b>ToCharArray<\/b> method. I then pipe the characters to the <b>Foreach-Object<\/b> cmdlet (<b>%<\/b> is an alias), and I use the letters from the word to perform a look up in the <b>$ltrFirst<\/b> hash table. The result outputs an array of numbers equal to the ASCII values of the word:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $w = &quot;dog&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">$w.ToCharArray() | % {$ltrFirst[&quot;$_&quot;]}<\/p>\n<p style=\"margin-left:30px\">100<\/p>\n<p style=\"margin-left:30px\">111<\/p>\n<p style=\"margin-left:30px\">103<\/p>\n<p><b>&nbsp; Note<\/b>&nbsp; Because I am looking up against strings, I need to use quotation marks around the <b>$_<\/b>, which contains <br \/>&nbsp; each letter from the word stored in <b>$w<\/b>.<\/p>\n<p>To be useful, I need to store the array of numbers in a variable, as shown here:<\/p>\n<p style=\"margin-left:30px\">$n = $w.ToCharArray() | % {$ltrFirst[&quot;$_&quot;]}&nbsp;&nbsp;&nbsp;<\/p>\n<h2>Decode<\/h2>\n<p>I want to be able to decode the numbers and convert them back into a word. To do this, I use the <b>$ASCIIFirst<\/b> hash table so that I can look up letters by their numeric value. I pipe the numeric array stored in the <b>$n<\/b> variable to the <b>Foreach-Object<\/b> cmdlet (<b>%<\/b> is the alias), and I use the number to look up by key in the <b>$ASCIIFirst<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $n | % {$ASCIIFirst[$_]}&nbsp;<\/p>\n<p style=\"margin-left:30px\">d<\/p>\n<p style=\"margin-left:30px\">o<\/p>\n<p style=\"margin-left:30px\">g<\/p>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">$c = $n | % {$ASCIIFirst[$_]}&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>If I look at <b>$c<\/b>, I will still see an array of letters, not an actual word. To put the pieces back together, I use the <b>&ndash;Join<\/b> operator:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $c -join &#039;&#039;&nbsp;<\/p>\n<p style=\"margin-left:30px\">dog<\/p>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">#AsciiEncodeDecodeWords.ps1<\/p>\n<p style=\"margin-left:30px\"># ed wilson<\/p>\n<p style=\"margin-left:30px\"># hsg-10-6,7-14<\/p>\n<p style=\"margin-left:30px\"># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">$ASCIIFirst=$ltrFirst=$null<\/p>\n<p style=\"margin-left:30px\">$ASCIIFirst = @{}<\/p>\n<p style=\"margin-left:30px\">$ltrFirst = @{}<\/p>\n<p style=\"margin-left:30px\">97..122 |<\/p>\n<p style=\"margin-left:30px\">&nbsp; Foreach-Object {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $ASCIIFirst.Add($_,([char]$_).ToString())}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">foreach($k in $ASCIIFirst.Keys)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; {$ltrFirst.add($ASCIIFirst[$k],$k)}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$w = &quot;dog&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">$w.ToCharArray() | % {$ltrFirst[&quot;$_&quot;]}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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p>\n<p style=\"margin-left:30px\">$c -join &#039;&#039;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[537,3,4,45],"class_list":["post-577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hash-tables","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=577"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}