<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mateusz Loskot &#187; binary</title>
	<atom:link href="http://mateusz.loskot.net/tag/binary/feed/" rel="self" type="application/rss+xml" />
	<link>http://mateusz.loskot.net</link>
	<description>mloskot&#039;s life, programming, c++, geo and adventures</description>
	<lastBuildDate>Tue, 10 Jan 2012 22:03:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WKB hex decoder in C++</title>
		<link>http://mateusz.loskot.net/2008/08/21/wkb-hex-decoder-in-c/</link>
		<comments>http://mateusz.loskot.net/2008/08/21/wkb-hex-decoder-in-c/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 15:53:56 +0000</pubDate>
		<dc:creator>mloskot</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[bytes]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[decode]]></category>
		<category><![CDATA[fdo]]></category>
		<category><![CDATA[fgf]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[ogc]]></category>
		<category><![CDATA[postgis]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[wkb]]></category>

		<guid isPermaLink="false">http://mateusz.loskot.net/?p=270</guid>
		<description><![CDATA[In PostGIS world, I often need to construct geometry from Well-Known-Binary (WKB) or PostGIS EWKB stream encoded as hex stream. It&#8217;s easy to do if I have access to PostgreSQL/PostGIS client which accepts SQL queries: SELECT ST_AsText( ST_GeomFromWKB( decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'), &#8230; <a href="http://mateusz.loskot.net/2008/08/21/wkb-hex-decoder-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.postgis.org/">PostGIS</a> world, I often need to construct geometry from Well-Known-Binary (WKB) or <a href="http://www.postgis.org/documentation/manual-1.3/ch04.html">PostGIS EWKB</a> stream encoded as hex stream. It&#8217;s easy to do if I have access to PostgreSQL/PostGIS client which accepts SQL queries:</p>
<pre><code>SELECT
   ST_AsText(
      ST_GeomFromWKB(
         decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'),
         4326))</code></pre>
</p>
<p>I often need to do the same directly in C++ code &#8211; parse hex encoded binary stream to raw stream of bytes. Here is simple hex decoder I use:</p>
<pre><code>#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
typedef std::vector&lt;unsigned char&gt; ewkb_t;

// bytes [out] - buffer for binary output
void hex_to_bytes(std::string const&#038; hexstr, ewkb_t&#038; bytes)
{
    bytes.clear();
    for(std::string::size_type i = 0; i < hexstr.size() / 2; ++i)
    {
        std::istringstream iss(hexstr.substr(i * 2, 2));
        unsigned int n;
        iss >> std::hex >> n;
        bytes.push_back(static_cast&lt;unsigned char&gt;(n));
    }
}
</code></pre>
</p>
<p>For example, I use it to build <a href="http://trac.osgeo.org/fdo/browser/trunk/Providers/PostGIS/Src/Provider/PgGeometry.h">FDO geometry objects</a> using another utility <a href="http://trac.osgeo.org/fdo/browser/trunk/Providers/PostGIS/Src/Provider/PgGeometry.cpp?rev=3904#L284">CreateGeometryFromExtendedWkb</a> defined in FDO provider for PostGIS:</p>
<pre><code>// POINT (1.234 5.678)
std::string ewkbhex("01010000005839B4C876BEF33F83C0CAA145B61640");
ewkb_t ewkb;
hex_to_bytes(ewkbhex, ewkb);
if (!ewkb.empty())
{
   FdoPtr&lt;fdoigeometry> g = CreateGeometryFromExtendedWkb(ewkb);
   // ... use geometry
}</code></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://mateusz.loskot.net/2008/08/21/wkb-hex-decoder-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

