{"id":78865,"date":"2016-06-20T00:01:08","date_gmt":"2016-06-20T07:01:08","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78865"},"modified":"2019-02-18T09:10:37","modified_gmt":"2019-02-18T16:10:37","slug":"build-a-hexadecimal-clock-in-powershell-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-a-hexadecimal-clock-in-powershell-part-1\/","title":{"rendered":"Build a hexadecimal clock in PowerShell \u2013 Part 1"},"content":{"rendered":"<p>Honorary Scripting Guy, Sean Kearney, is here today, and I\u2019m going to sit down and have some fun this week.<\/p>\n<p>Today I was feeling a bit bored and, for some reason, the thought \u201chexadecimal clock\u201d entered my head. Don\u2019t ask why because I\u2019m not quite sure about the \u201cWhy\u201d part. But, it just did.<\/p>\n<p>Then, I sat down and thought, what\u2019s involved? Really?<\/p>\n<p>As a script, from a pseudo-code standpoint, it\u2019s pretty simple<\/p>\n<ul>\n<li>Get current time<\/li>\n<li>Obtain hours, minutes, and seconds<\/li>\n<li>Convert each item to a two-character hexadecimal number<\/li>\n<li>Write each character to the screen<\/li>\n<li>Loop over and over and over and over<\/li>\n<\/ul>\n<p>Only I wanted this to be\u2026well\u2026b-i-g. In my head, I wanted a clock as <u>wide<\/u> as my console. That started my head spinning. \u201cBig letters. How can I draw big letters and numbers?\u201d<\/p>\n<p>In the \u201cold world\u201d before the Internet, before Snapchat, before 10 Megabyte hard drives, there were those of us that lived in the world of Commodore and Atari. Some of us were geeky enough to draw custom fonts in an 8-bit by 8-bit box.<\/p>\n<p>That was my world at one point. I thought it was pretty neat.<\/p>\n<p>In the PowerShell world, I sat down and thought about it. I could easily make the same thing in the console.<\/p>\n<p>So, I began to draw a zero on the screen using nothing but the zero character like the following example:<\/p>\n<p style=\"padding-left: 60px\">\u00a0000000\n00\u00a0\u00a0 000\n00\u00a0 0 00\n00 00 00\n00 0\u00a0 00\n000\u00a0\u00a0 00\n000000<\/p>\n<p>Feeling pretty pleased with my most excellent artwork, I began to think about how to store it. \u201cAn Array! I\u2019ll make this character an array!\u201d<\/p>\n<p>My first thought was to create a multi-dimensional array to access the individuals rows and characters:<\/p>\n<p style=\"padding-left: 60px\">$HexArray=New-Object \u2018object[,]\u2019 7,17<\/p>\n<p>Then, for each character in the array. I would do something like this:<\/p>\n<p style=\"padding-left: 60px\">$Hexarray[0,0]=\u2019 000000 \u2018\n$Hexarray[1,0]=\u201900\u00a0\u00a0 000\u2019\n$Hexarray[2,0]=\u201900\u00a0 0 00\u2019\n$Hexarray[3,0]=\u201900 00 00\u2019\n$Hexarray[4,0]=\u201900 0\u00a0 00\u2019\n$Hexarray[5,0]=\u2019000\u00a0\u00a0 00\u2019\n$Hexarray[6,0]=\u2019 000000\u2019<\/p>\n<p>I would continue with something like this for each character, changing the final position with a 1 and then 2.<\/p>\n<p>But, a little voice said to me, \u201cToo ugly, too complex.\u201d It really wasn\u2019t a hard solution but <u>visually<\/u>, I wanted to just \u201cEdit the characters without working around quotes and defined objects.\u201d<\/p>\n<p>In this situation, a <strong>Here-String<\/strong> would be perfect. I could define a single array and have each member be a single string that\u2019s defined as a <strong>Here-String<\/strong>.<\/p>\n<p>First step, define an array to hold 18 characters, one for each of the 16 Hexadecimal characters and two for an optional character like a hyphen or separator for the numbers.<\/p>\n<p style=\"padding-left: 60px\">[array]$HexArray=@(&#8216; &#8216;) *18<\/p>\n<p>Now at this point, I can simply define each of the characters. I decided on a simply sequence: a number match for each hex character in its sequence from 0 to 15 (0 to F).<\/p>\n<p>To define the characters, it began to look like this:<\/p>\n<p style=\"padding-left: 60px\">$HexArray[0]=@&#8217;\n000000\n00\u00a0\u00a0 000\n00\u00a0 0 00\n00 00 00\n00 0\u00a0 00\n000\u00a0\u00a0 00\n000000\n&#8216;@\n$HexArray[1]=@&#8217;\n11\n1111\n11 11\n11\n11\n11\n11111111<\/p>\n<p style=\"padding-left: 60px\">&#8216;@<\/p>\n<p>This process continued until I had the various characters defined. With this done, I could now pull up the hexadecimal character \u2018f\u2019 by using its numeric position. When done, I would get an oversized novelty version like this!<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062016.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78875\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062016.png\" alt=\"Screenshot of the hexadecimal character \u2018f\u2019 by using its numeric position.\" width=\"373\" height=\"165\" \/><\/a><\/p>\n<p>Now the brain began to churn. How could I get the time to mix into this? For that, return for tomorrow\u2019s Hey Scripting Guy episode!<\/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>Honorary Scripting Guy, Sean Kearney, is here today, and I\u2019m going to sit down and have some fun this week. Today I was feeling a bit bored and, for some reason, the thought \u201chexadecimal clock\u201d entered my head. Don\u2019t ask why because I\u2019m not quite sure about the \u201cWhy\u201d part. But, it just did. Then, [&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-78865","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>Honorary Scripting Guy, Sean Kearney, is here today, and I\u2019m going to sit down and have some fun this week. Today I was feeling a bit bored and, for some reason, the thought \u201chexadecimal clock\u201d entered my head. Don\u2019t ask why because I\u2019m not quite sure about the \u201cWhy\u201d part. But, it just did. Then, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78865","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=78865"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78865\/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=78865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}