<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/ip-string-to-integer-conversion-with-rcpp/"></div></div><div><a data-url="http://www.r-bloggers.com/ip-string-to-integer-conversion-with-rcpp/" data-counturl="http://www.r-bloggers.com/ip-string-to-integer-conversion-with-rcpp/" data-count="horizontal" data-text="IP string to integer conversion with Rcpp" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://opiateforthemass.es/articles/IPintegers/"> Opiate for the masses</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>IP address conversion</h2>
<p><span>A</span>t work I recently had to match data on IP addresses and some fuzzy timestamp matching – a mess, to say the least. But before I could even tackle that problem, one dataset had the IPs stored as a character (e.g. <code>10.0.0.0</code>), while the other dataset had the IP addresses converted as integers (e.g. <code>167772160</code>).</p>
<p>Storing IPs as integers has the advantage of saving some space and making calculations easier. <a target="_blank" href="http://www.aboutmyip.com/AboutMyXApp/IP2Integer.jsp?ipAddress=10.0.0.0">This page</a> goes into detail on how this conversion is made. You split the IP address into the four octets and then shift each octet by sets of 8 bit:</p>
<p> (first octet * 256³) + (second octet * 256²) + (third octet * 256) + (fourth octet)</p>
<p>Using <code>10.0.0.0</code> as an example:</p>
<p><code>(10*256^3) + (0*256^2) + (0*256^1) + (0*256^0) = 167772160</code></p>
<h2>Converting in R</h2>
<p>Since it’s a simple mathematical conversion, it’s easy to write a function that will convert the IP to integer, and also back. Stackoverflow has <a target="_blank" href="http://stackoverflow.com/questions/21610147/convert-ip-address-ipv4-itno-an-integer-in-r">an answer here</a>.</p>
<h2>Converting in Rcpp</h2>
<p>During my googleing, I stumbled across <a target="_blank" href="http://datadrivensecurity.info/blog/posts/2014/May/speeding-up-ipv4-address-conversion-in-r/">this blogpost</a>, which solved the problem with some CPP code using the magic of <a target="_blank" href="http://www.boost.org/">boost</a>, a CPP library with lots of nice functions.</p>
<p>Since my data had several millions of rows, generally anything that speeds up conversions is a good idea! I tried the code available at their site which threw some errors on comment characters. After removing the comments, everything worked nicely (don’t forget to install the boost libraries! <code>sudo apt-get install libboost-dev</code>):</p>
<div>
<pre><code><span>#include &lt;Rcpp.h> #include &lt;boost/asio/ip/address_v4.hpp>
</span>
<span>using</span> <span>namespace</span> <span>Rcpp</span><span>;</span>

<span>// [[Rcpp::export]]
</span><span>unsigned</span> <span>long</span> <span>rinet_pton</span> <span>(</span><span>CharacterVector</span> <span>ip</span><span>)</span> <span>{</span> 
  <span>return</span><span>(</span><span>boost</span><span>::</span><span>asio</span><span>::</span><span>ip</span><span>::</span><span>address_v4</span><span>::</span><span>from_string</span><span>(</span><span>ip</span><span>[</span><span>0</span><span>]).</span><span>to_ulong</span><span>());</span>
<span>}</span>

<span>// [[Rcpp::export]]
</span><span>CharacterVector</span> <span>rinet_ntop</span> <span>(</span><span>unsigned</span> <span>long</span> <span>addr</span><span>)</span> <span>{</span>
  <span>return</span><span>(</span><span>boost</span><span>::</span><span>asio</span><span>::</span><span>ip</span><span>::</span><span>address_v4</span><span>(</span><span>addr</span><span>).</span><span>to_string</span><span>());</span>
<span>}</span>
</code></pre>
</div>
<p>Running the code in R is simple, and you’ll get the result without any problems:</p>
<div>
<pre><code><span>library</span><span>(</span><span>Rcpp</span><span>)</span>
<span>library</span><span>(</span><span>inline</span><span>)</span>
<span>Rcpp</span><span>::</span><span>sourceCpp</span><span>(</span><span>&quot;iputils.cpp&quot;</span><span>)</span>

<span># test convert an IPv4 string to integer
</span><span>rinet_pton</span><span>(</span><span>&quot;10.0.0.0&quot;</span><span>)</span>
<span>#[1] 167772160
</span>
<span># test conversion back
</span><span>rinet_ntop</span><span>(</span><span>167772160</span><span>)</span>
<span>#[1] &quot;10.0.0.0&quot;
</span></code></pre>
</div>
<p>Unfortunately, the result returned is a scalar. Running the command in a <code>mutate()</code> only returns the first IP for all rows.<br>
So, I took to vectorising the code. Time to grab the excellent <a target="_blank" href="http://adv-r.had.co.nz/">advanced R</a> website/book by Hadley. Specifically, the <a target="_blank" href="http://adv-r.had.co.nz/Rcpp.html">Rcpp section</a>. Checking the cpp code, I noticed that <code>rinet_pton</code> returns a scalar (<code>unsigned long</code>), even though a vector is used as an input (<code>CharacterVector</code>). Moreover, it will always pick the first IP from the character vector input to return: <code>from_string(ip[0])</code>.</p>
<p>Going by the Rcpp documentation, I changed the inputs and returns to vectors always, and wrote a quick cpp loop to vectorise the functions.</p>
<div>
<pre><code><span>// [[Rcpp::export]]
</span><span>IntegerVector</span> <span>rinet_pton</span> <span>(</span><span>CharacterVector</span> <span>ip</span><span>)</span> <span>{</span> 
  <span>int</span> <span>n</span> <span>=</span> <span>ip</span><span>.</span><span>size</span><span>();</span>
  <span>IntegerVector</span> <span>out</span><span>(</span><span>n</span><span>);</span>
  
  <span>for</span><span>(</span><span>int</span> <span>i</span> <span>=</span> <span>0</span><span>;</span> <span>i</span> <span>&lt;</span> <span>n</span><span>;</span> <span>++</span><span>i</span><span>)</span> <span>{</span>
    <span>out</span><span>[</span><span>i</span><span>]</span> <span>=</span> <span>boost</span><span>::</span><span>asio</span><span>::</span><span>ip</span><span>::</span><span>address_v4</span><span>::</span><span>from_string</span><span>(</span><span>ip</span><span>[</span><span>i</span><span>]).</span><span>to_ulong</span><span>();</span>
  <span>}</span>
  <span>return</span> <span>out</span><span>;</span>
<span>}</span>


<span>// [[Rcpp::export]]
</span><span>CharacterVector</span> <span>rinet_ntop</span> <span>(</span><span>IntegerVector</span> <span>addr</span><span>)</span> <span>{</span>
  <span>int</span> <span>n</span> <span>=</span> <span>addr</span><span>.</span><span>size</span><span>();</span>
  <span>CharacterVector</span> <span>out</span><span>(</span><span>n</span><span>);</span>
  
  <span>for</span><span>(</span><span>int</span> <span>i</span> <span>=</span> <span>0</span><span>;</span> <span>i</span> <span>&lt;</span> <span>n</span><span>;</span> <span>++</span><span>i</span><span>)</span> <span>{</span>
    <span>out</span><span>[</span><span>i</span><span>]</span> <span>=</span> <span>boost</span><span>::</span><span>asio</span><span>::</span><span>ip</span><span>::</span><span>address_v4</span><span>(</span><span>addr</span><span>[</span><span>i</span><span>]).</span><span>to_string</span><span>();</span>
  <span>}</span>
  <span>return</span> <span>out</span><span>;</span>
<span>}</span>
</code></pre>
</div>
<p>With the functions now vectorised, it’s easy to pass vectors, and run the function on a dataframe column.</p>
<div>
<pre><code><span>rinet_pton</span><span>(</span><span>c</span><span>(</span><span>&quot;10.0.0.0&quot;</span><span>,</span> <span>&quot;192.168.0.1&quot;</span><span>))</span>
<span># [1] 167772160 -1062731775
</span></code></pre>
</div>
<p>I should note that I know nothing of cpp programming, and this was fully hacked by following the Rcpp examples. The <code>rinet_ntop()</code> function throws an error on passing negative numbers (it expects an unsigned long), so you can’t reconvert the <code>192.168.0.1</code> IP to integer and back. This was not a problem for me, since all I needed was to match the IPs, and my integer IPs in the one dataset were created via boost in the first place.</p>
<p>The code is available on github as <a target="_blank" href="https://gist.github.com/safferli/70a858a460c0a084e35bcb71bc214273">a gist</a>.</p>
<p><a target="_blank" href="http://opiateforthemass.es/articles/IPintegers/">IP string to integer conversion with Rcpp</a> was originally published by Kirill Pomogajko at <a target="_blank" href="http://opiateforthemass.es/articles/IPintegers/">Opiate for the masses</a> on May 19, 2016.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/ip-string-to-integer-conversion-with-rcpp/"></div></div><div><a data-url="http://www.r-bloggers.com/ip-string-to-integer-conversion-with-rcpp/" data-counturl="http://www.r-bloggers.com/ip-string-to-integer-conversion-with-rcpp/" data-count="vertical" data-text="IP string to integer conversion with Rcpp" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://opiateforthemass.es/articles/IPintegers/"> Opiate for the masses</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gZVG-EGbA9k"></a></div>