Build a hexadecimal clock in PowerShell – Part 1


Honorary Scripting Guy, Sean Kearney, is here today, and I’m going to sit down and have some fun this week.

Today I was feeling a bit bored and, for some reason, the thought “hexadecimal clock” entered my head. Don’t ask why because I’m not quite sure about the “Why” part. But, it just did.

Then, I sat down and thought, what’s involved? Really?

As a script, from a pseudo-code standpoint, it’s pretty simple

  • Get current time
  • Obtain hours, minutes, and seconds
  • Convert each item to a two-character hexadecimal number
  • Write each character to the screen
  • Loop over and over and over and over

Only I wanted this to be…well…b-i-g. In my head, I wanted a clock as wide as my console. That started my head spinning. “Big letters. How can I draw big letters and numbers?”

In the “old world” 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.

That was my world at one point. I thought it was pretty neat.

In the PowerShell world, I sat down and thought about it. I could easily make the same thing in the console.

So, I began to draw a zero on the screen using nothing but the zero character like the following example:

 000000
00   000
00  0 00
00 00 00
00 0  00
000   00
000000

Feeling pretty pleased with my most excellent artwork, I began to think about how to store it. “An Array! I’ll make this character an array!”

My first thought was to create a multi-dimensional array to access the individuals rows and characters:

$HexArray=New-Object ‘object[,]’ 7,17

Then, for each character in the array. I would do something like this:

$Hexarray[0,0]=’ 000000 ‘
$Hexarray[1,0]=’00   000’
$Hexarray[2,0]=’00  0 00’
$Hexarray[3,0]=’00 00 00’
$Hexarray[4,0]=’00 0  00’
$Hexarray[5,0]=’000   00’
$Hexarray[6,0]=’ 000000’

I would continue with something like this for each character, changing the final position with a 1 and then 2.

But, a little voice said to me, “Too ugly, too complex.” It really wasn’t a hard solution but visually, I wanted to just “Edit the characters without working around quotes and defined objects.”

In this situation, a Here-String would be perfect. I could define a single array and have each member be a single string that’s defined as a Here-String.

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.

[array]$HexArray=@(' ') *18

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).

To define the characters, it began to look like this:

$HexArray[0]=@'
000000
00   000
00  0 00
00 00 00
00 0  00
000   00
000000
'@
$HexArray[1]=@'
11
1111
11 11
11
11
11
11111111

'@

This process continued until I had the various characters defined. With this done, I could now pull up the hexadecimal character ‘f’ by using its numeric position. When done, I would get an oversized novelty version like this!

Screenshot of the hexadecimal character ‘f’ by using its numeric position.

Now the brain began to churn. How could I get the time to mix into this? For that, return for tomorrow’s Hey Scripting Guy episode!

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

Until then, always remember that with Great PowerShell comes Great Responsibility.

Sean Kearney
Honorary Scripting Guy
Cloud and Datacenter Management MVP

Comments (0)

Skip to main content