<?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>hton &#8211; 编程技术记录</title>
	<atom:link href="https://blog.z6z8.cn/tag/hton/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.z6z8.cn</link>
	<description>世界你好!</description>
	<lastBuildDate>Tue, 12 Nov 2019 02:24:50 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>大小端转换的C++ 封装 ：hton ， ntol</title>
		<link>https://blog.z6z8.cn/2019/11/12/%e5%a4%a7%e5%b0%8f%e7%ab%af%e8%bd%ac%e6%8d%a2%e7%9a%84c-%e5%b0%81%e8%a3%85-%ef%bc%9ahton-%ef%bc%8c-ntol/</link>
					<comments>https://blog.z6z8.cn/2019/11/12/%e5%a4%a7%e5%b0%8f%e7%ab%af%e8%bd%ac%e6%8d%a2%e7%9a%84c-%e5%b0%81%e8%a3%85-%ef%bc%9ahton-%ef%bc%8c-ntol/#respond</comments>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Tue, 12 Nov 2019 02:24:50 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[代码片段]]></category>
		<category><![CDATA[hton]]></category>
		<category><![CDATA[ntoh]]></category>
		<category><![CDATA[大小端]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=560</guid>

					<description><![CDATA[使用C++的模板、自动类型推导技术 #ifndef HtoN_h #define HtoN_h #includ [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>使用C++的模板、自动类型推导技术</p>
<pre><code class="language-cpp">#ifndef  HtoN_h
#define HtoN_h

#include &lt;arpa/inet.h&gt;

#if __ANDROID__
    #ifndef htonll
        #define htonll(x) htonq(x)
    #endif

    #ifndef ntohll
        #define ntohll(x) ntohq(x)
    #endif
#endif

namespace zxszl
{
    template &lt;typename T&gt; T zl_hton (T t) { return t;}
    template &lt;&gt; inline uint16_t zl_hton&lt;uint16_t&gt; (uint16_t t) { return htons(t); }
    template &lt;&gt; inline int16_t zl_hton&lt;int16_t&gt; (int16_t t) { return htons(t); }
    template &lt;&gt; inline uint32_t zl_hton&lt;uint32_t&gt; (uint32_t t) { return htonl(t); }
    template &lt;&gt; inline int32_t zl_hton&lt;int32_t&gt; (int32_t t) { return htonl(t); }
    template &lt;&gt; inline uint64_t zl_hton&lt;uint64_t&gt; (uint64_t t) { return htonll(t); }
    template &lt;&gt; inline int64_t zl_hton&lt;int64_t&gt; (int64_t t) { return htonll(t); }

    template &lt;typename T&gt; T zl_ntoh (T t) { return t;}
    template &lt;&gt; inline uint16_t zl_ntoh&lt;uint16_t&gt; (uint16_t t) { return ntohs(t); }
    template &lt;&gt; inline int16_t zl_ntoh&lt;int16_t&gt; (int16_t t) { return ntohs(t); }
    template &lt;&gt; inline uint32_t zl_ntoh&lt;uint32_t&gt; (uint32_t t) { return ntohl(t); }
    template &lt;&gt; inline int32_t zl_ntoh&lt;int32_t&gt; (int32_t t) { return ntohl(t); }
    template &lt;&gt; inline uint64_t zl_ntoh&lt;uint64_t&gt; (uint64_t t) { return ntohll(t); }
    template &lt;&gt; inline int64_t zl_ntoh&lt;int64_t&gt; (int64_t t) { return ntohll(t); }

    class __judge_little_endian
    {
    public:
        static inline bool isle() { static const int _a = 0xAABBCCDD; return *(unsigned char *)(&amp;_a) == 0xDD;}
    };

#define zl_rorder_s(x) \
            ((__uint16_t)((((__uint16_t)(x) &amp; 0xff00) &gt;&gt; 8) | \
            (((__uint16_t)(x) &amp; 0x00ff) &lt;&lt; 8)))

#define zl_rorder_l(x) \
            ((__uint32_t)((((__uint32_t)(x) &amp; 0xff000000) &gt;&gt; 24) | \
            (((__uint32_t)(x) &amp; 0x00ff0000) &gt;&gt;  8) | \
            (((__uint32_t)(x) &amp; 0x0000ff00) &lt;&lt;  8) | \
            (((__uint32_t)(x) &amp; 0x000000ff) &lt;&lt; 24)))

#define zl_rorder_ll(x) \
            ((__uint64_t)((((__uint64_t)(x) &amp; 0xff00000000000000ULL) &gt;&gt; 56) | \
            (((__uint64_t)(x) &amp; 0x00ff000000000000ULL) &gt;&gt; 40) | \
            (((__uint64_t)(x) &amp; 0x0000ff0000000000ULL) &gt;&gt; 24) | \
            (((__uint64_t)(x) &amp; 0x000000ff00000000ULL) &gt;&gt;  8) | \
            (((__uint64_t)(x) &amp; 0x00000000ff000000ULL) &lt;&lt;  8) | \
            (((__uint64_t)(x) &amp; 0x0000000000ff0000ULL) &lt;&lt; 24) | \
            (((__uint64_t)(x) &amp; 0x000000000000ff00ULL) &lt;&lt; 40) | \
            (((__uint64_t)(x) &amp; 0x00000000000000ffULL) &lt;&lt; 56)))

    template &lt;typename T&gt; T zl_htole (T t) { return t;}
    template &lt;&gt; inline uint16_t zl_htole&lt;uint16_t&gt; (uint16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);}
    template &lt;&gt; inline int16_t zl_htole&lt;int16_t&gt; (int16_t t) {return __judge_little_endian::isle() ? t :  zl_rorder_s(t);}
    template &lt;&gt; inline uint32_t zl_htole&lt;uint32_t&gt; (uint32_t t) {return __judge_little_endian::isle() ? t :  zl_rorder_l(t);}
    template &lt;&gt; inline int32_t zl_htole&lt;int32_t&gt; (int32_t t) {return __judge_little_endian::isle() ? t :  zl_rorder_l(t);}
    template &lt;&gt; inline uint64_t zl_htole&lt;uint64_t&gt; (uint64_t t) { return __judge_little_endian::isle() ? t :  zl_rorder_ll(t); }
    template &lt;&gt; inline int64_t zl_htole&lt;int64_t&gt; (int64_t t) { return __judge_little_endian::isle() ? t :  zl_rorder_ll(t); }

    template &lt;typename T&gt; T zl_letoh (T t) { return t;}
    template &lt;&gt; inline uint16_t zl_letoh&lt;uint16_t&gt; (uint16_t t) {return __judge_little_endian::isle() ? t : zl_rorder_s(t);}
    template &lt;&gt; inline int16_t zl_letoh&lt;int16_t&gt; (int16_t t) {return __judge_little_endian::isle() ? t :  zl_rorder_s(t);}
    template &lt;&gt; inline uint32_t zl_letoh&lt;uint32_t&gt; (uint32_t t) {return __judge_little_endian::isle() ? t :  zl_rorder_l(t);}
    template &lt;&gt; inline int32_t zl_letoh&lt;int32_t&gt; (int32_t t) {return __judge_little_endian::isle() ? t :  zl_rorder_l(t);}
    template &lt;&gt; inline uint64_t zl_letoh&lt;uint64_t&gt; (uint64_t t) { return __judge_little_endian::isle() ? t :  zl_rorder_ll(t); }
    template &lt;&gt; inline int64_t zl_letoh&lt;int64_t&gt; (int64_t t) { return __judge_little_endian::isle() ? t :  zl_rorder_ll(t); }

#undef zl_rorder_s
#undef zl_rorder_l
#undef zl_rorder_ll
}

#endif /* HtoN_h */</code></pre>
<p>使用示例</p>
<pre><code class="language-cpp">using namespace zxszl;

short m = 10;
short n = zl_hton(m);
m = zl_ntoh(n);

int x = 100;
int y = zl_hton(x);
x = zl_ntoh(y);</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.z6z8.cn/2019/11/12/%e5%a4%a7%e5%b0%8f%e7%ab%af%e8%bd%ac%e6%8d%a2%e7%9a%84c-%e5%b0%81%e8%a3%85-%ef%bc%9ahton-%ef%bc%8c-ntol/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
