{"id":12751,"date":"2011-09-09T00:01:00","date_gmt":"2011-09-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/09\/09\/convert-hexadecimal-to-ascii-using-powershell\/"},"modified":"2011-09-09T00:01:00","modified_gmt":"2011-09-09T00:01:00","slug":"convert-hexadecimal-to-ascii-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/convert-hexadecimal-to-ascii-using-powershell\/","title":{"rendered":"Convert Hexadecimal to ASCII Using PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Superhero BATCHman defeats evil Hextor and converts hexadecimal to ASCII to retrieve passwords.\n&nbsp;\nMicrosoft Scripting Guy Ed Wilson here. Sean Kearney is back again, and this time he delivers Episode 7 of the BATCHman series.\n&nbsp;\n<a href=\"http:\/\/i.technet.microsoft.com\/bb410849.Batchman_Icon_05_bluebatchman_082511(en-us,MSDN.10).png\"><img decoding=\"async\" style=\"max-width: 550px\" border=\"0\" alt=\"\" src=\"http:\/\/i.technet.microsoft.com\/bb410849.Batchman_Icon_05_bluebatchman_082511(en-us,MSDN.10).png\"><\/a>\nWhenever trouble happens in systems and people will call,\nAnd darkness rolls out causing your fall,\nCreatures of bits roam in the night,\nShine to the sky, the bright bluish Light,\nAnd call to&hellip;BATCHman !\n&hellip;and, oh yes, his sidekick Boy Blunder Cmdlet, too.\n&nbsp;\n<b>In Today&rsquo;s Episode, BATCHman Encounters HEXTOR the Encyptor!<\/b>\nPanic grips the city! A beacon cuts through the night! It&rsquo;s the BATCHsignal!\n&ldquo;Beep! Beep! beep! Beep!&rdquo; Cmdlet rolls over and executes the <b>Get-Snooze<\/b> script. It is naptime in the BATCHcave.\nBut not for BATCHman. He deftly grabs Cmdlet&rsquo;s blanket and flips him into the air like a flapjack. &ldquo;No time to waste! Evil is afoot! Quick! To the Winmobile!&rdquo;\nCmdlet looks inside the BATCHbay and sees an empty parking spot.\n&ldquo;Uhhhh, BATCHman, where&rsquo;s the Winmobile?&rdquo; gesturing to a large empty rectangle of pavement.\nBATCHman smacks his head . &ldquo;D&rsquo;oh! I forgot I shipped it out for a Mango upgrade! Whatever shall we do?&rdquo;\n&ldquo;Holy, Big Ben! What horrible timing! Then to the BATCHsubway! Quick, grab the tokens!&rdquo;\nThey disappear into the local mass transit system.\n<b>***Meanwhile in another part of the city***<\/b>\n&ldquo;BAA FAA FAA FAA!&rdquo; the evil villain cackles &ldquo;BAA FAA FAA!&rdquo;\nDressed in nothing but dark purple with a large hexagon for a symbol, the evil genius Hextor looks down upon the chaos that has become the local Redmond post office, smiling.\n&ldquo;BAA FAA FAA! All your passwords are belong to me! The systems are locked away. None of you will ever retrieve them! For I have encrypted everything in&hellip;.HEXADECIMAL! BAA FAA FAAAA!&rdquo;\nThe administrator looks shaken. Decoding hexadecimal! What a foul turn of events! &ldquo;Oh if only BATCHman were here!&rdquo;\nAs if by sheer luck, our fearless heroes stumble onto the scene, smelling vaguely of the subway.\nHextor looks at our heroes with disdain. &ldquo;Nothing can save them. Not you or your silly bad acting sidekick! Everything is written in pure hexadecimal! BAA FAAA FAAA FAAAA!&rdquo; he giggled madly\nCmdlet stares at Hextor&rsquo;s copy of the user password list. Now written in hexadecimal, it seems impossible to decrypt. &ldquo;Holy magic hexes, BATCHman! He wasn&rsquo;t kidding!&rdquo;<\/p>\n<p style=\"padding-left: 30px\">50 6f 73 74 61 6c 41 63 63 65 73 73 31 09 55 62 65 72 4c 33 33 74 50 40 73 73 77 30 72 64 0d 0a 53 75 70 65 72 76 69 73 6f 72 09 42 40 74 63 68 4d 40 6e 77 31 6c 6e 76 72 47 33 74 74 68 31 73 21 0d 0a 41 64 6d 69 6e 69 73 74 72 61 74 6f 72 09 21 4b 72 40 35 68 4e 62 75 52 4e 3f 0d 0a 50 6f 73 74 61 6c 41 63 63 65 73 73 32 09 42 75 61 68 21 48 41 3f 68 61 21 48 41 41 41 41 21 3f 21 0d 0a\n&ldquo;Why, foul villian, I believe you&rsquo;re right. Nothing can solve this easily. Nothing that is,&rdquo; BATCHman pauses dramatically with a finger in the air, &ldquo;except Windows PowerShell!&rdquo;\nHextor&rsquo;s eyes open like flying saucers! &ldquo;EEEEEEEEEEEEEEEEEEEEE! AAAAAAAAAA! BAAAAA! &rdquo; is all that escapes his mouth as he runs off hands in the air, immediately sensing defeat.\nBATCHman looks over at Cmdlet. &ldquo;In Windows PowerShell, we can easily convert binary, octal, decimal, and even hexadecimal. It is a native feature. We just need to leverage <b>[CONVERT]<\/b>. To quickly convert a number to hexadecimal, I would type this.<\/p>\n<p style=\"padding-left: 30px\">[convert]::tostring(12345,16)\n&ldquo;This will produce the following hexadecimal output.<\/p>\n<p style=\"padding-left: 30px\">3039\n&ldquo;To convert back to decimal, we specify that we&rsquo;re converting to an integer and specify the base we&rsquo;re converting from.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">[convert]::toint16(&ldquo;3039&rdquo;,16)\nWhich produces<\/p>\n<p style=\"padding-left: 30px\">12345\nCmdlet looked on. &ldquo;Wow, BATCHman! But that doesn&#8217;t help us. We need to convert those hexadecimal numbers to decimal and then convert them back to a real ASCII character! I don&rsquo;t think we can convert a number to an ASCII character. That&rsquo;s just lu&hellip;&rdquo;\n&ldquo;Hold there, chum. We can do that as well in Windows PowerShell. We just need to type it as a <b>[char]<\/b>. When <b>[char]<\/b> is followed by any decimal number less than 256, it converts it to an ASCII character. We can even find the ASCII value of characters using the <b>[BYTE]<\/b> type like this.<\/p>\n<p style=\"padding-left: 30px\">[BYTE][CHAR]&rsquo;a&rsquo;<\/p>\n<p style=\"padding-left: 30px\">97\n&ldquo;To convert back, all we do is reverse the process.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">[CHAR][BYTE]97<\/p>\n<p style=\"padding-left: 30px\">a\nCmdlet looked at the string of hexadecimal numbers. &ldquo;So if we convert these to decimal, we can just preface the result of each one with <b>[CHAR][BYTE]<\/b> and see its ASCII character? That sounds too easy.&rdquo;\n&ldquo;You&rsquo;ve got it on the nose, oh sidekick of mine! Now, the challenge is we need as easy way to split up those hexadecimal pairs to convert them. Any thoughts?&rdquo;\nThe eager one claps his hands together &ldquo;Split! Holy chopping board, BATCHman! We can take this data, drop it into a string, and run a <b>split<\/b> method based upon the spaces separating them!&rdquo;\nCmdlet quickly sets up a simple variable in Windows PowerShell:<\/p>\n<p style=\"padding-left: 30px\">$HEXDATA=&rdquo;50 6f 73 74 61 6c 41 63 63 65 73 73 31 09 55 62 65 72 4c 33 33 74 50 40 73 73 77 30 72 64 0d 0a 53 75 70 65 72 76 69 73 6f 72 09 42 40 74 63 68 4d 40 6e 77 31 6c 6e 76 72 47 33 74 74 68 31 73 21 0d 0a 41 64 6d 69 6e 69 73 74 72 61 74 6f 72 09 21 4b 72 40 35 68 4e 62 75 52 4e 3f 0d 0a 50 6f 73 74 61 6c 41 63 63 65 73 73 32 09 42 75 61 68 21 48 41 3f 68 61 21 48 41 41 41 41 21 3f 21 0d 0a&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$HEXDATA.Split(&ldquo; &ldquo;)\n&ldquo;What next? I think you have the idea right!&rdquo;\n&ldquo;Well, we could pipe this into <b>ForEach<\/b> and apply the <b>[convert]::toint16()<\/b> against each like this.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$HEXDATA.Split(&ldquo; &ldquo;) | FOREACH{ [CONVERT]::toint16($_,16)}\nBATCHman rubs his hands together with glee as the decimal numbers pour down the screen. &ldquo;Excellent, Cmdlet! Now with all the decimal numbers, you can just convert them to ASCII, right?&rdquo;\nThe other half of the terrible twosome added the <b>[CHAR][BYTE]<\/b> to the output.<\/p>\n<p style=\"padding-left: 30px\">$HEXDATA.Split(&ldquo; &ldquo;) | FOREACH {[BYTE][CHAR]([CONVERT]::toint16($_,16))}\nHis jaw dropped. &ldquo;Oh, no, it&rsquo;s just a long unreadable column of letters. I need to write it in a row.&rdquo;\n&ldquo;Soooooo, what if we could write to the host with no new line? We can pass your data to the <b>Write-Host<\/b> cmdlet without dumping a line feed between each out like so.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$HEXDATA.Split(&ldquo; &ldquo;) | FOREACH {WRITE-HOST &ndash;object ( [BYTE][CHAR]([CONVERT]::toint16($_,16))) &ndash;nonewline }\nCmdlet stared in awe as the message on the screen converted back to ASCII.<\/p>\n<p style=\"padding-left: 30px\">PostalAccess1 UberL33tP@ssw0rd <br \/>Supervisor B@tchM@nw1lnvrG3tth1s! <br \/>Administrator !Kr@5hNbuRN? <br \/>PostalAccess2 Buah!HA?ha!HAAAA!?!\n&ldquo;Now, quickly get the credentials reset before Hextor strikes again!&rdquo;\nQuickly the credentials were reset and the Redmond post office was back online. The postmaster stepped out to shake their hands.\n&ldquo;Oh, BATCHman, thank you! How can we ever repay you now that you&rsquo;ve returned our passwords to us?&rdquo;\n&ldquo;No thanks necessary, but any chance you have a stamp? I have to mail this to my mother for her birthday.&rdquo;\n&nbsp;\nThanks, Sean, for another awesome episode of BATCHman. Everyone, join us tomorrow for Episode 8.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto: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.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b>\n&nbsp;\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Superhero BATCHman defeats evil Hextor and converts hexadecimal to ASCII to retrieve passwords. &nbsp; Microsoft Scripting Guy Ed Wilson here. Sean Kearney is back again, and this time he delivers Episode 7 of the BATCHman series. &nbsp; Whenever trouble happens in systems and people will call, And darkness rolls out causing your fall, Creatures [&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":[284,56,3,4,154,21,45],"class_list":["post-12751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-batchman","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-string-manipulation","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Superhero BATCHman defeats evil Hextor and converts hexadecimal to ASCII to retrieve passwords. &nbsp; Microsoft Scripting Guy Ed Wilson here. Sean Kearney is back again, and this time he delivers Episode 7 of the BATCHman series. &nbsp; Whenever trouble happens in systems and people will call, And darkness rolls out causing your fall, Creatures [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12751","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=12751"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12751\/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=12751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}