Converting IP to Binary and Back!

This week on “I learn a weird, semi-useless skill” brings you how I learn to convert 32 bit IP addresses into their binary counter part and back. If I asked you if you have ever or would ever need this knowledge, you’d probably say no. But the learning must continue!

Firstly, lets pick an IP address, this can be any random combination of 4 pairs of 3 digits between 0 and 255. For this guide, I’ll be using 176.42.6.213. For no apparent reason.

So to convert this number into binary, I use a simple table method. You can see the table below:

128 64 32 16 8 4 2 1

This table makes it very simple to work out how to convert each of the four octets in an IP address to binary.

First we start with 176

We go through each of the numbers in the table above and see if the column number can be subtracted from the first octet of the 32-bit address. So our first step would be, can we subtract 128 from 176?

The answer is YES so I would put a “1” in the 128 column, and our remainder from the above sum is “48“. Your table should look similar to the one below.

128 64 32 16 8 4 2 1
 1

Now we can do the next step using our remainder of “48“. Can we subtract 64 from 48?

The answer is NO so I would put a “0” in the 64 column. When the answer is no, then the remainder stays the same, so the next sum would be, can we subtract 32 from 48? This goes on until you are at the end of the table and your remainder should be 0.

The finished table for the 176 part is shown below:

128 64 32 16 8 4 2 1
1  0  1  1  0  0  0  0

Once you have done this for all 4 octets in the IP address, you should end up with 4 tables filled with 1’s and 0’s. The full binary number for our IP address is:

10110000.00101010.00000110.11010101

Converting Binary to the 32-bit IP address is quite a lot simpler. Once again we use the same table but this time we put the binary number into the table and read which numbers have a 1 and which numbers have a 0 in the column.

For example, plugging the binary of “176” into the table gives us the below result:

128 64 32 16 8 4 2 1
1 0 1 1 0 0 0 0

From this we can see that the following columns have a 1 assigned to them:

  • 128
  • 32
  • 16

Now all we have to do is add these numbers together to get the first octet of the 32-bit address, which is “176“. Now do this process for the other 3 parts of the binary address and you will get back the original IP address of “176.42.6.213

I would suggest doing this exercise multiple times to really nail it if you need to. Who knows, if you and your company are really sad and nerdy, this might be a nice party trick. Or a particularly cringey 15 minutes.

I think I explained this well enough. If not, please comment and let me know what parts I could improve or add. Enjoy!

P.S You have also want to use this form I created to do this activity: Converting 32-Bit Address to Binary and Back

Mailto in SharePoint String Builder

Bit of a weird, one off sort of thing. I was creating a workflow in SharePoint which would send an email. On this email would be an “Approve” and “Decline” button. The decline button was easy enough to do but the approve button turned out to be a complete pain in the back side.

First of all, I would like to tell you to not use mailto whenever you can. There are better programs out there. Trust me. If anyone knows any really good ones that they standby please feel free to leave a comment.

The mailto syntax is basically this:

mailto:RECIPIENT?cc=CCRECIPIENT&subject=SUBJECT TEXT&body=BODY TEXT

This looks simple enough but once you want to start doing some more complicated features/formatting that would otherwise be quite simple in HTML, become near impossible here. Especially with it being in a SharePoint string builder box.

What I wanted was for there to be three lines of text, two in which got information from the SharePoint form to fill in the information and one for adding text. By the way if you didn’t know, to add a new line you can use “%0d0d“.

That’s: percent sign – zero – delta – zero – delta

This is the final code that I ended up with:

mailto:RECIPIENT@EMAIL.COM?cc=[%Current Item:Created By%]&subject=Approval Authorisation for [%Current Item:Created By%]&body=The remote access request for [%Current Item:Created By%] has been approved by [%Current item:Manager's Name%]%0d%0dThe reason for approval is - [%Current Item:Reason for approval%]%0d%0dPlease specify any limitations below:

Here is a picture just in case you want to see it and below that what the actual email looks like:

String Builder

Email

I would like to add that you will have to change the “Add or Change Lookup” so that it finds the “Display Name”, otherwise the users will show as the default SharePoint format which isn’t as good looking. You can see this below:

Display Name

Hope you enjoyed and found this useful. I may to an entire blog trying to outline the possibilities of using the SharePoint string builder and “Define E-Mail Message” features. Suppose you’ll have to wait and see.

Creating a Dynamically Changing WinForm

This is a fairly simple thing, if you know what you’re doing or if you have done it before. But if, like me, you are at a complete lost and sick of searching only to find half complete or even completely contradictory answers, then this is the guide for you.

My goals for this GUI was to have the form resize IF a tick box was $true (ticked)

First I started off created a blank form with a single checkbox item, you can see this below:

$form = New-Object System.Windows.Forms.Form
$form.Text = "New User Form"
$form.Size = New-Object System.Drawing.Size(500,540)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "Fixed3D"
$form.MaximizeBox = $false
$form.MinimizeBox = $false

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(165,470)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(245,470)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Location = New-Object System.Drawing.Point(100,100)
$checkbox.Size = New-Object System.Drawing.Size(100,25)
$checkbox.Text = "Test Checkbox"
$form.controls.Add($checkbox)

$result = $form.ShowDialog()

That will create an empty form like the one below:

Initial Form

Now I will add the following code after all the objects but before the end $result variable which displays the form:

$checkbox.add_checkstatechanged({

 if ($checkbox.Checked -eq $True){

  $form.size = new-object system.drawing.size(800,540)

 }else{

  $form.size = new-object system.drawing.size(500,540)

}

})

 

this is the part that will make the form larger if the checkbox is checked and return it to the original size if it isn’t checked. You can do SO many other things as well. For example, having a textbox enabled or disabled, changing the forms title, changing the colour of the form and to be honest just about anything you can think of.

This is the complete script you will need:

$form = New-Object System.Windows.Forms.Form
$form.Text = "New User Form"
$form.Size = New-Object System.Drawing.Size(500,540)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "Fixed3D"
$form.MaximizeBox = $false
$form.MinimizeBox = $false

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(165,470)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(245,470)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Location = New-Object System.Drawing.Point(100,100)
$checkbox.Size = New-Object System.Drawing.Size(100,25)
$checkbox.Text = "Test Checkbox"
$form.controls.Add($checkbox)

$checkbox.add_checkstatechanged({
 if ($checkbox.Checked -eq $true){
  $form.Size = New-Object System.Drawing.Size(800,540)
 }else{
  $form.Size = New-Object System.Drawing.Size(500,540)
 }
})

$result = $form.ShowDialog()

This script will create a form like in the screenshots below. In this screenshot the checkbox is not ticked:

Initial Form

And in this screenshot the checkbox has been ticked, making the form size noticeably wider:

Ticked Form

Hope this helps someone struggling with getting started with some of the more complex WinForm tricks. Enjoy and comment if you need any help!