<?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"
	>

<channel>
	<title>DEAN LEE:/DEV/BLOG &#187; convert</title>
	<atom:link href="http://www.deanlee.cn/tag/convert/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deanlee.cn</link>
	<description>mount /dev/brain &#124;&#124; tail -f /var/log/thoughts &#62;&#62; /pub/www</description>
	<pubDate>Fri, 30 Dec 2011 13:27:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
	<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Convert Ip Address To Geographic Location</title>
		<link>http://www.deanlee.cn/programming/convert-ip-address-to-geographic-location/</link>
		<comments>http://www.deanlee.cn/programming/convert-ip-address-to-geographic-location/#comments</comments>
		<pubDate>Wed, 11 Oct 2006 18:27:34 +0000</pubDate>
		<dc:creator>Dean Lee</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[convert]]></category>

		<category><![CDATA[ipaddress]]></category>

		<category><![CDATA[project]]></category>

		<category><![CDATA[qqwry]]></category>

		<guid isPermaLink="false">http://www.deanlee.cn/2006/10/13/convert-ip-address-to-geographic-location-2/</guid>
		<description><![CDATA[基于C++的CIPSeeker类，查找IP所在的地理位置,同时为了方便在其他脚本语言中提供IP地理位置查找功能（asp或者C#,vb等），另作一个COM控件来包装这个类。]]></description>
			<content:encoded><![CDATA[<p>网上有不少关于如何使用<a href="http://www.cz88.net/fox/" target="_blank">纯真IP数据库</a>(QQWRY.dat),来查找IP所在的地理位置的文档和源代码，但基本都是java实现，很少有C的，而且大部分都使用了打开文件句柄，通过文件指针的多次跳转来实现单次查找，运行效率很低，尤其是在短时间内需要执行大量IP-&gt;地理位置的转换的场合，这种方式的运行性能基本是难以接受的。</p>
<p><span id="more-82"></span></p>
<p>因此我写了一个c++的CIPSeeker类，该类在初始化时将qqwry.dat装载进内存，之后所有的查找都通过内存指针来进行。同时为了方便在其他脚本语言中提供IP地理位置查找功能（asp或者C#,vb等），另作了一个COM控件来包装这个类。</p>
<p>出于把问题简单化和性能上的考虑，该C++类不支持多线程并发。如果要在多线程环境下并发查找的话，为每个要使用IPSeeker功能的的线程创建一个单独的CIPSeeker的实例就可以了。封装的com控件使用apartment线程模式，所以不用担心多线程问题。</p>
<h3>使用方法</h3>
<p>先下载最新版本的<a href="http://www.cz88.net/fox/" target="_blank">纯真IP数据库</a>，解压缩之后将QQWRY.dat拷贝到应用程序同一目录下（如果是使用com控件，拷贝到com控件所在的目录。</p>
<div class="dean_ch" style="white-space: wrap;">CIPSeeker theSeeker;<br />
theSeeker.<span class="me1">Create</span><span class="br0">&#40;</span><span class="br0">&#40;</span>HINSTANCE<span class="br0">&#41;</span>GetModuleHandle<span class="br0">&#40;</span><span class="kw2">NULL</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw4">char</span> szLocation<span class="br0">&#91;</span><span class="nu0">256</span><span class="br0">&#93;</span>;<br />
theSeeker.<span class="me1">getIPLocation</span><span class="br0">&#40;</span><span class="st0">&quot;202.101.103.55&quot;</span>, szLocation, _countof<span class="br0">&#40;</span>szLocation<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;the Location is:%s&quot;</span>, szLocation<span class="br0">&#41;</span>;</div>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">Set</span> obj = <span class="kw1">server</span>.<span class="kw3">CreateObject</span><span class="br0">&#40;</span><span class="st0">&quot;IPToLocation.Seeker&quot;</span><span class="br0">&#41;</span><br />
<span class="kw1">Response</span>.<span class="kw3">Write</span><span class="br0">&#40;</span> obj.<span class="me1">GetLocation</span><span class="br0">&#40;</span><span class="st0">&quot;202.101.103.55&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
<h3>Revision History</h3>
<ul>
<li>CIPSeeker 0.1: 初始化版本.</li>
</ul>
<h3>C++源代码</h3>
<ul>
<li><a href="http://www.deanlee.cn/wp-content/uploads/ipseeker.cpp" title="ipseeker.cpp">IPSeeker.cpp</a>  (源文件)</li>
<li><a href="http://www.deanlee.cn/wp-content/uploads/ipseeker.h" title="ipseeker.h">IPSeeker.h</a>   (头文件)</li>
</ul>
<h3>COM控件</h3>
<ul>
<li><a href="http://www.deanlee.cn/wp-content/uploads/iptolocation.zip">IPToLocation.dll</a> (COM 控件)</li>
<li><a href="http://www.deanlee.cn/wp-content/uploads/iptolocation_prj.zip">IPToLocation_Project</a>(COM控件的项目源代码)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.deanlee.cn/programming/convert-ip-address-to-geographic-location/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Convert Unicode To UTF8</title>
		<link>http://www.deanlee.cn/programming/convert-unicode-to-utf8/</link>
		<comments>http://www.deanlee.cn/programming/convert-unicode-to-utf8/#comments</comments>
		<pubDate>Sun, 24 Sep 2006 06:00:15 +0000</pubDate>
		<dc:creator>Dean Lee</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[c]]></category>

		<category><![CDATA[code_snippet]]></category>

		<category><![CDATA[convert]]></category>

		<category><![CDATA[unicode]]></category>

		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://www.deanlee.cn/2006/09/24/convert-unicode-to-utf8/</guid>
		<description><![CDATA[char* __stdcall UnicodeToUtf8&#40; const WCHAR* wstr &#41;
&#123;
&#160; &#160; const WCHAR* w;
&#160; &#160; // Convert unicode to utf8
&#160; &#160; int len = 0;
&#160; &#160; for &#40; w = wstr; *w; w++ &#41; &#123;
&#160; &#160; &#160; &#160; if &#40; *w &#60; 0&#215;0080 &#41; len++;
&#160; &#160; &#160; &#160; else if &#40; *w &#60; 0&#215;0800 &#41; len += 2;
&#160; [...]]]></description>
			<content:encoded><![CDATA[<div class="dean_ch" style="white-space: wrap;"><span class="kw4">char</span>* __stdcall UnicodeToUtf8<span class="br0">&#40;</span> <span class="kw4">const</span> WCHAR* wstr <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">const</span> WCHAR* w;<br />
&nbsp; &nbsp; <span class="co1">// Convert unicode to utf8</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> len = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> w = wstr; *w; w++ <span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> *w &lt; 0&#215;0080 <span class="br0">&#41;</span> len++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span> *w &lt; 0&#215;0800 <span class="br0">&#41;</span> len += <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> len += <span class="nu0">3</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw4">unsigned</span> <span class="kw4">char</span>* szOut = <span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span>* <span class="br0">&#41;</span>malloc<span class="br0">&#40;</span> len<span class="nu0">+1</span> <span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> szOut == <span class="kw2">NULL</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">NULL</span>;</p>
<p>&nbsp; &nbsp; <span class="kw4">int</span> i = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> w = wstr; *w; w++ <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> *w &lt; 0&#215;0080 <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szOut<span class="br0">&#91;</span>i++<span class="br0">&#93;</span> = <span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> <span class="br0">&#41;</span> *w;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span> *w &lt; 0&#215;0800 <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szOut<span class="br0">&#91;</span>i++<span class="br0">&#93;</span> = 0xc0 | <span class="br0">&#40;</span><span class="br0">&#40;</span> *w <span class="br0">&#41;</span> &gt;&gt; <span class="nu0">6</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szOut<span class="br0">&#91;</span>i++<span class="br0">&#93;</span> = 0&#215;80 | <span class="br0">&#40;</span><span class="br0">&#40;</span> *w <span class="br0">&#41;</span> &amp;amp; 0&#215;3f <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szOut<span class="br0">&#91;</span>i++<span class="br0">&#93;</span> = 0xe0 | <span class="br0">&#40;</span><span class="br0">&#40;</span> *w <span class="br0">&#41;</span> &gt;&gt; <span class="nu0">12</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szOut<span class="br0">&#91;</span>i++<span class="br0">&#93;</span> = 0&#215;80 | <span class="br0">&#40;</span><span class="br0">&#40;</span> <span class="br0">&#40;</span> *w <span class="br0">&#41;</span> &gt;&gt; <span class="nu0">6</span> <span class="br0">&#41;</span> &amp;amp; 0&#215;3f <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; szOut<span class="br0">&#91;</span>i++<span class="br0">&#93;</span> = 0&#215;80 | <span class="br0">&#40;</span><span class="br0">&#40;</span> *w <span class="br0">&#41;</span> &amp;amp; 0&#215;3f <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; szOut<span class="br0">&#91;</span> i <span class="br0">&#93;</span> = <span class="st0">&#8216;<span class="es0">\0</span>&#8216;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span> <span class="kw4">char</span>* <span class="br0">&#41;</span>szOut;<br />
<span class="br0">&#125;</span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.deanlee.cn/programming/convert-unicode-to-utf8/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

