{"id":78925,"date":"2016-06-22T00:01:55","date_gmt":"2016-06-22T07:01:55","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78925"},"modified":"2019-02-18T09:10:36","modified_gmt":"2019-02-18T16:10:36","slug":"build-a-hexadecimal-clock-in-powershell-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-a-hexadecimal-clock-in-powershell-part-3\/","title":{"rendered":"Build a hexadecimal clock in PowerShell \u2013 Part 3"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use Select-String in PowerShell to identify data and match it to an array.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here today to play more with our funky, nerdy, cool hexadecimal clock.<\/p>\n<p>Yesterday we built out a small function named Get-HexTime to give us the current time that displays hex digits instead of decimal for hours, minutes, and seconds.<\/p>\n<p>One of our challenges is going to be matching up the individual hex digits to our positions in the array. We can step through the list of characters in <strong>HexTime<\/strong> with a pretty simple loop:<\/p>\n<p style=\"padding-left: 90px\"><code>$Hextime=Get-Hextime\nFor ($Count=0; $Count -lt ($HexTime.Length); $Count++)\n{\n$Character=$HexTime[$count]\n}<\/code><\/p>\n<p>With this loop in place, I just need to figure out what the characters were and maybe convert them to a numeric value to access the various elements of the array.<\/p>\n<p>At first, I thought to myself, I\u2019ll just find out what the ASCII character is by using the following:<\/p>\n<p style=\"padding-left: 90px\"><code>[byte][char]\u20190\u2019\n[byte][char]\u2019a\u2019<\/code><\/p>\n<p>This process reveals the ASCII code of the character. The numeric characters would be 0 to 9 (and in sequence) with values of 48 to 57.<\/p>\n<p>The letters, a \u2013 f, would be 65 to 71. Of course, additional characters would have to be trapped for. My initial thoughts were that I could use the PowerShell switch statement and have it return the matching value in the following manner:<\/p>\n<p style=\"padding-left: 90px\"><code>switch ($Character=$HexTime[$count])\n{<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>'0' { $HexValue=0 }\n'1' { $HexValue=1 }<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>...<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>'a' { $HexValue=10 }<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>...<\/code><\/p>\n<p style=\"padding-left: 90px\"><code>'f' { $HexValue=15 }\n}<\/code><\/p>\n<p>You get the idea. But, first off, I hate to type repetitive information. Then I thought on a different line. I could take the sequence and pipe it into <strong>Select-String<\/strong> to find a match.<\/p>\n<p>In the following example, I can have <strong>Select-String<\/strong> look for the \u2018e\u2019 and return an object that contains the exact position it was found in.<\/p>\n<p style=\"padding-left: 90px\"><code>\u20180123456789abcdef\u2019 | Select-String -Pattern 'e'<\/code><\/p>\n<p>Although the result seems to only be the same original string, if you pipe this to <strong>Get-Member<\/strong>, you\u2019ll see some different properties. More importantly, you see a property named \u201cMatches\u201d.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062216.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78935\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062216.png\" alt=\"Screenshot that shows the Matches property among other properties.\" width=\"530\" height=\"340\" \/><\/a><\/p>\n<p>If you access this property directly, you\u2019ll see something really cool, a deeper property named <strong>Index<\/strong>, which is the exact position in the string where this value was found.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062216.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78945\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062216.png\" alt=\"Screenshot that shows the Index property.\" width=\"184\" height=\"145\" \/><\/a><\/p>\n<p>Because each character in our string is <u>unique<\/u> in both value and position, we can use this to match against our array.<\/p>\n<p>Now here is how we could pull out the location of the value in the array:<\/p>\n<p style=\"padding-left: 90px\"><code># Get the Character from the String\n$Character=$Hextime[$Count]<\/code><\/p>\n<p style=\"padding-left: 90px\"><code># Find what position it exists in\n$Match=('0123456789abcdef\/-' | Select-String \u2013pattern $Character)<\/code><\/p>\n<p style=\"padding-left: 90px\"><code># Get that exact value\n$Index=$Match.Matches.Index<\/code><\/p>\n<p>To make it simpler, we could wrap it all up in one line like this:<\/p>\n<p style=\"padding-left: 90px\"><code>$Index=('0123456789abcdef\/-' | Select-String \u2013pattern ($Hextime[$Count])).matches.index<\/code><\/p>\n<p>Now that we\u2019ve gotten all the hard work out of the way, tomorrow we\u2019ll really have fun as we start to draw this output onto the screen!<\/p>\n<p>I invite you to follow the Scripting Guys 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 them 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.<\/p>\n<p>Until then always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Select-String in PowerShell to identify data and match it to an array. Honorary Scripting Guy, Sean Kearney, is here today to play more with our funky, nerdy, cool hexadecimal clock. Yesterday we built out a small function named Get-HexTime to give us the current time that displays hex digits instead of decimal for [&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":[568,685,641],"tags":[56,154,45],"class_list":["post-78925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Select-String in PowerShell to identify data and match it to an array. Honorary Scripting Guy, Sean Kearney, is here today to play more with our funky, nerdy, cool hexadecimal clock. Yesterday we built out a small function named Get-HexTime to give us the current time that displays hex digits instead of decimal for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78925","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=78925"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78925\/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=78925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}