{"id":101,"date":"2010-03-17T14:35:40","date_gmt":"2010-03-17T14:35:40","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=101"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"converting-ip-addresses","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/tcp-ip\/converting-ip-addresses","title":{"rendered":"Converting IP Addresses"},"content":{"rendered":"<h4>\nIP Address Text Entry Box<br \/>\n<\/h4>\n<p>\nYou can use a MaskedTextBox with the mask &quot;###.###.###.###&quot;<br \/>\nHowever its often best just to use a standard text box as the masked text box approah creates issues. Note that the Windows network setup IP address box is not based on a standard tool unfortunately.\n<\/p>\n<h4>\nParsing Removing Leading Zero&#39;s<br \/>\n<\/h4>\n<p>\nIPAddress::TryParse doesn&#39;t accept leading zeros&#39;s (as it deals with them as octals &#8211; bloody stupid but there you go).<br \/>\nSo you have to remove them. Do this:\n<\/p>\n<pre>\r\n<code>\r\n\tString ^EnteredIpAddress;\r\n\tString ^ParseIpAddress;\r\n\tSystem::Net::IPAddress ^IpAddress;\r\n\r\n\tEnteredIpAddress = txtIpAddress-&gt;Text-&gt;Replace(&quot; &quot;, &quot;0&quot;);\t\/\/For masked text box convert spaces to zero&#39;s\r\n\r\n\t\/\/Remove any leading zero&#39;s for the parse function (which treats them as octal markers)\r\n\tParseIpAddress = &quot;.&quot; + EnteredIpAddress;\t\t\t\/\/Add a leading &quot;.&quot;\r\n\tParseIpAddress = ParseIpAddress-&gt;Replace(&quot;.0&quot;, &quot;.&quot;);\t\/\/Remove a leading zero\r\n\tParseIpAddress = ParseIpAddress-&gt;Replace(&quot;.0&quot;, &quot;.&quot;);\t\/\/Remove a leading zero\r\n\tParseIpAddress = ParseIpAddress-&gt;Replace(&quot;..&quot;, &quot;.0.&quot;);\t\t\/\/Correct any bytes which we&#39;re zero\r\n\tParseIpAddress = ParseIpAddress-&gt;Substring(1);\t\t\/\/Remove the leading &quot;.&quot; again\r\n\tif (!System::Net::IPAddress::TryParse(ParseIpAddress, IpAddress))\r\n\t\tIpAddress = IPAddress::Parse(&quot;0.0.0.0&quot;);\r\n<\/code><\/pre>\n<h5>\nAlternatively, as a function to call<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/***************************************\r\n\t\/\/***************************************\r\n\t\/\/********** FORMAT IP ADDRESS **********\r\n\t\/\/***************************************\r\n\t\/\/***************************************\r\n\tprivate: String ^FormatIpAddress (String ^EnteredIpAddress)\r\n\t{\r\n\t\tString ^ParseIpAddress;\r\n\t\tSystem::Net::IPAddress ^IpAddress;\r\n\r\n\t\tEnteredIpAddress = EnteredIpAddress-&gt;Replace(&quot; &quot;, &quot;&quot;);\r\n\r\n\t\t\/\/Remove any leading zero&#39;s\r\n\t\tParseIpAddress = &quot;.&quot; + EnteredIpAddress;\t\t\t\/\/Add a leading &quot;.&quot;\r\n\t\tParseIpAddress = ParseIpAddress-&gt;Replace(&quot;.0&quot;, &quot;.&quot;);\t\/\/Remove a leading zero\r\n\t\tParseIpAddress = ParseIpAddress-&gt;Replace(&quot;.0&quot;, &quot;.&quot;);\t\/\/Remove a leading zero\r\n\t\tParseIpAddress = ParseIpAddress-&gt;Replace(&quot;..&quot;, &quot;.0.&quot;);\t\t\/\/Correct any bytes which we&#39;re zero\r\n\t\tParseIpAddress = ParseIpAddress-&gt;Substring(1);\t\t\/\/Remove the leading &quot;.&quot; again\r\n\t\tif (!System::Net::IPAddress::TryParse(ParseIpAddress, IpAddress))\r\n\t\t\tIpAddress = IPAddress::Parse(&quot;0.0.0.0&quot;);\r\n\r\n\t\treturn(IpAddress-&gt;ToString());\r\n\t}\r\n<\/code><\/pre>\n<h4>\nConnverting from a IPAddress to a String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tsTemp = IpAddress1-&gt;ToString();\r\n<\/code><\/pre>\n<h4>\nConnverting from a string to IP Address<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tIPAddress ^address;\r\n\tif (!IPAddress::TryParse(OurIpString, address))\r\n\t\taddress = IPAddress::Parse(&quot;255.255.255.255&quot;);\t\/\/Error - use default broadast address\r\n<\/code><\/pre>\n<h4>\nConnverting from a IP Address To Bytes<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tIPEndPoint^ tempIpEP = gcnew IPEndPoint(SourceIpAddress,0);\r\n\tEndPoint^ tempRemoteEP = safe_cast&lt;EndPoint^&gt;(tempIpEP);\r\n\tSocketAddress^ ThisSocket = tempRemoteEP-&gt;Serialize();\r\n\tif (ThisSocket-&gt;Family == AddressFamily::InterNetwork)\r\n\t{\r\n\t\tPort = Convert::ToInt16(ThisSocket[2]) &lt;&lt; 8;\t\/\/Port number is in bytes 2:3\r\n\t\tPort |= Convert::ToInt16(ThisSocket[3]);\r\n\r\n\t\trxIpBytes[0] = ThisSocket[4];\t\/\/IP Address in bytes 4:7\r\n\t\trxIpBytes[1] = ThisSocket[5];\r\n\t\trxIpBytes[2] = ThisSocket[6];\r\n\t\trxIpBytes[3] = ThisSocket[7];\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>IP Address Text Entry Box You can use a MaskedTextBox with the mask &quot;###.###.###.###&quot; However its often best just to use a standard text box as the masked text box approah creates issues. Note that the Windows network setup IP address box is not based on a standard tool unfortunately. Parsing Removing Leading Zero&#39;s IPAddress::TryParse [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-tcp-ip"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":16,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":944,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/101\/revisions\/944"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}