A Reminder To Self

This is a techie post (just a heads up) and certainly one my wife will enjoy. She asks me how to do this exact thing all the time!

In all seriousness, this is one of those things I just know I’ll forget…again. What I’ll end up doing is recreating this table to refresh myself on basic number conversions. I’m going to push it out on my blog so I can refer to it in the future and quit this nonsense!!

Binary, Decimal, Octal, oh my

If you are like me, you rarely need to convert Hex into other formats…such as binary. But I thought I would give myself (and now you) an explanation that helps me remember.

Let’s look at 3 easy to use examples of the same decimal value represented in various forms.

Binary (base 2)
0 or 1​
Decimal (base 10)
0 through 9​
Octal (base 8)
0 through 7​
Hex (base 16)​
0 through 9, A (10) through F (15)
00000000​ 0 0 00​
00001111​ 15​ 17​ 0F​
11111111​ 256​ 400​ 100

Let’s examine the last row in this table, in expanded form. This will help us prove all these representations are the same just in different bases. And we’ll convert everything to decimal since we’re most familiar with it.

11111111 = 1*2^7 + 1*2^6 + 1*2^5 + 1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 256

256 = 2*10^2 + 5*10^1 + 6*10^0 = 256

400 = 4*8^2 + 0*8^1 + 0*8^0 = 256

100 = 1*16^2 + 0*16^1 + 0*16^0 = 256

So it would be easy enough to come up with a new base 17, that has digits A through Q as its character base. I’m sure it does exist already, but you get the idea.

Hopefully this will help you in exploring areas like powershell, where you can look at code like this:

$mac = [byte[]](“F4-CE-46-B6-1C-2C”.split(‘-‘) |% {[int]”0x$_”})

and it makes perfect sense why the next command:

$mac

produces this:

244
206
70
182
28
44

If that wasn’t immediately obvious: this takes the mac address in hex format and does a split on it by that delimiter. For each piece that was split, it’s fed into $_ which is the latest animal on the stack. That is cast/converted from a 0x__ hex format to an int. A C# byte (used by .Net and hence powershell) is an unsigned 8-bit integer from 0 to 255 so technically the explicit cast to an int is unecessary. For mac addresses, the highest numerical value you would expect to see on the output stack is 255 which is the highest hex value FF. In this case, % symbol is a shortcut for “For-Each” in powershell syntax. But now you see how conversions work.

And all of that helps when you’re trying to do something like this (remotely asking a computer to wake up using a mac address). You have to ask since let’s face it, sometimes computers get sleepy and don’t respond too well.

$mac = [byte[]](“F4-CE-46-B6-1C-2C”.split(‘-‘) |% {[int]”0x$_”})
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$packet = [byte[]](,0xFF * 102)
6..101 |% { $packet[$_] = $mac[($_%6)]}
$UDPclient.Send($packet, $packet.Length)

This has absolutely nothing to do with the post, but I think it's a great picture, so I included it. You're welcome.

This has absolutely nothing to do with the post, but I think it’s a great picture, so I included it. You’re welcome.

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: