{"id":12121,"date":"2011-11-11T00:01:00","date_gmt":"2011-11-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/11\/use-powershell-to-easily-convert-decimal-to-binary-and-back\/"},"modified":"2011-11-11T00:01:00","modified_gmt":"2011-11-11T00:01:00","slug":"use-powershell-to-easily-convert-decimal-to-binary-and-back","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-easily-convert-decimal-to-binary-and-back\/","title":{"rendered":"Use PowerShell to Easily Convert Decimal to Binary and Back"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to use Windows PowerShell to easily convert decimal to binary and back, and simplify custom subnet mask calculations.<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Microsoft Scripting Guy Ed Wilson here. Today is 63. Yep, that is right, today is 63. That is what you get when you see 11 11 11 represented as binary. This appears here.<\/span><\/span><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"99\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">32<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">16<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">8<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">4<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">2<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"93\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"99\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"106\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<td valign=\"top\" width=\"93\">\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1<\/span><\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">PS C:\\&gt; 1+2+4+8+16+32<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">63<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">A long time ago in this same galaxy, I wrote a VBScript function that would accept a binary number such as 111111 and translate it to a decimal value. It was a fun exercise, but it was also a bit complex. I had to get the length of the string, break it down piece by piece by position, calculate the value of that position, keep a running total, and loop back again. Now, I could translate that VBScript into Windows PowerShell, but that would be both a waste of time as well as downright misleading. This is because in Windows PowerShell, I have direct access to the .NET Framework <b>Convert<\/b><i> <\/i>class. The <b>Convert<\/b><i> <\/i>class resides in the <b>system<\/b> namespace, and it contains a large number of static methods for converting one type of thing to another. Well, okay, it contains 25 static methods and properties (I can use the <b>Get-Member<\/b> cmdlet and the <b>Measure-Object<\/b> cmdlet to obtain this information): <\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">PS C:\\&gt; [convert] | gm -s | measure<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Count&nbsp;&nbsp;&nbsp; : 25<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Average&nbsp; :<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Maximum&nbsp; :<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Minimum&nbsp; :<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\"><span>Property :<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dsfy6sz9.aspx\"><span style=\"font-family: Segoe\">Convert class is well documented on MSDN<\/span><\/a><span><span style=\"font-family: Segoe\">, but in reality, the information obtained via the <b>Get-Member<\/b> cmdlet is usually enough information to make the conversion. For example, I can put the <b>Convert<\/b> class in square brackets, pipe it to <b>Get-Member<\/b>, use the <b>static<\/b><i> <\/i>switch to retrieve static members, list the exact method I want to use, and send it to the <b>Format-List<\/b> cmdlet. This will show the method and all the associated overloads. This command sounds complicated, but it is really simple. The command is shown here:<\/span><\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">[convert] | gm -s toint32 | fl *<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The command and associated output are shown in the following figure.<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7065.hsg-11-11-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7065.hsg-11-11-11-1.png\" \/><\/a><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Hmm, it looks like there is a static method called <b>ToInt32<\/b> that will accept a string value, so to use this method to convert a binary number, all I need to do is to pass the string and the number base (which is base 2 for the case of binary number conversion). The command shown here translates today&rsquo;s date, 111111:<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">[convert]::ToInt32(&#8220;111111&#8221;,2)<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The command and associated output are shown here.<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2642.hsg-11-11-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2642.hsg-11-11-11-2.png\" \/><\/a><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I can use the following script to translate a binary formatted subnet mask into decimal format:<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><b><span><span style=\"font-family: Segoe\">ConvertBinarySubnetMaskToDecimal.ps1<\/span><\/span><\/b><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">$a=$i=$null<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&#8220;11111111&#8221;,&#8221;11111111&#8243;,&#8221;11111000&#8243;,&#8221;00000000&#8243; | <\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;% {<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; $i++<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; [string]$a += [convert]::ToInt32($_,2)<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; if($i -le 3) {[string]$a += &#8220;.&#8221;}<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp; }<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">$a<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">ConvertBinarySubnetMaskToDecimal.ps1 demonstrates using the <b>System.Convert<\/b> .NET Framework class to convert from a binary number into decimal format. It uses a counter variable, <b>$i<\/b>, to keep track of decimal placement. As each number passes over the pipeline, I store the converted value in the <b>$a<\/b> variable. This script could easily be converted into a function if this were a task that is normally performed. The complete text of this script appears on the <\/span><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Convert-Binary-Subnet-Mask-52f45694\"><span style=\"font-family: Segoe\">Scripting Guys Script Repository<\/span><\/a><span><span style=\"font-family: Segoe\">. <\/span><\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The flip side of the coin is translating a decimal number into binary format. To convert a decimal number into binary format, I once again use the <b>Convert<\/b> class, but this time, I use the <b>ToString<\/b> method. The syntax is similar to that of the <b>ToInt32<\/b><i> <\/i>method. The command shown here converts the decimal number 15 into a binary number.<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">[convert]::ToString(15,2)<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">The command and associated output appear here.<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">PS C:\\&gt; [convert]::ToString(15,2)<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">1111<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">After I know I can do that, I can also write a quick script to convert a decimal subnet mask to binary representation. This script is shown here:<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><b><span><span style=\"font-family: Segoe\">ConvertDecimalSubnetMaskToBinary.ps1<\/span><\/span><\/b><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">$a=$i=$null<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&#8220;255&#8221;,&#8221;255&#8243;,&#8221;128&#8243;,&#8221;0&#8243; | <\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;% {<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; $i++<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; [string]$a += [convert]::ToString([int32]$_,2)<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp;&nbsp;&nbsp; if($i -le 3) {[string]$a += &#8220;.&#8221;}<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">&nbsp;&nbsp; }<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\">$a<\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">ConvertDecimalSubnetMaskToBinary.ps1 demonstrates using the <b>System.Convert<\/b> .NET Framework class to convert from a decimal number into binary format. It uses a counter variable, <b>$i<\/b>, to keep track of decimal placement. As each number passes over the pipeline, I store the converted value in the $a variable. This script could easily be converted into a function if this were a task that is normally performed. The complete text of this script appears on the <\/span><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Convert-decimal-subnet-8f44965a\"><span style=\"font-family: Segoe\">Scripting Guys Script Repository<\/span><\/a><span><span style=\"font-family: Segoe\">. <\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span>Join me tomorrow as I introduce Guest Blogger Chris Walker who will talk about using Windows PowerShell to manage SharePoint profiles. It is a really good article, and I am certain you will enjoy it. Until then, see ya. <\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"font-family: Segoe\">Twitter<\/span><\/a><span style=\"font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"font-family: Segoe\">scripter@microsoft.com<\/span><\/a><span style=\"font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><span><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><\/span><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to easily convert decimal to binary and back, and simplify custom subnet mask calculations. &nbsp; Microsoft Scripting Guy Ed Wilson here. Today is 63. Yep, that is right, today is 63. That is what you get when you see 11 11 11 represented as binary. This appears here. [&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":[1],"tags":[25,37,3,4,298,45],"class_list":["post-12121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-networking","tag-scripting-guy","tag-scripting-techniques","tag-tcpip","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to easily convert decimal to binary and back, and simplify custom subnet mask calculations. &nbsp; Microsoft Scripting Guy Ed Wilson here. Today is 63. Yep, that is right, today is 63. That is what you get when you see 11 11 11 represented as binary. This appears here. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12121","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=12121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12121\/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=12121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}