C# 大端与小端
<div id="article_content" class="article_content clearfix"><div class="htmledit_views" id="content_views">
<div class="tit" style="font-family: Arial; font-size: 14px; line-height: 26px">c# 通信中字节序处理。</div>
<div class="table-box"><table style="color: rgba(0, 0, 0, 1); font-family: Arial; font-size: 14px; line-height: 26px; table-layout: fixed"><tbody><tr><td>
<div id="blog_text" class="cnt">
<p>原文地址:https://blog.csdn.net/ybhjx/article/details/50475440<p\>
</p\></p><p>
最近在写一个短信下发功能,客户端使用c#和java的短信网关的进行网络通信。</p>
<p>
之前使用java进行开发,一切正常,改用c#无法收到网关应答。</p>
<p>
想了半天意识到是不是网络字节序问题,</p>
<p>
<span style="font-size: 18px">java默认就是大端字节序,和网络字节序是一至的,所以不转换也不会有问题,</span></p>
<p>
而c#在windows平台上是小端字节序。</p>
<p>
网络发送字节流是按大端序发送,也就是从左到右发送,和c#的小端序相反,造成网关不能正常识别协议。</p>
<p>
尝试c#中转换一下字节序,通信成功。</p>
<p>
c#中字节序转换有两种方法。</p>
<p>
非字串使用 System.BitConverter.GetBytes()方法,先读入字节数组中,然后再用Array.Reverse()对byte数组反序一下,得到大端序字节数组。</p>
<p>
代码: </p>
<p>
short x = 6;</p>
<p>
byte[] a=System.BitConverter.GetBytes(x); //得到小端字节序数组</p>
<p>
Array.Reverse(a); //反转数组转成大端。</p>
<p>
</p>
<p>
</p>
<p>
另外c#直接提供了网络字节序转换方法。</p>
<p>
System.Net.IPAddress.HostToNetworkOrder(本机到网络转换)</p>
<p>
System.Net.IPAddress.NetworkToHostOrder(网络字节转成本机)</p>
<p>
推荐使用这种方法,简单有效。</p>
<p>
代码示例:</p>
<p>
short x = 6;</p>
<p>
short b = System.Net.IPAddress.HostToNetworkOrder(x); //把x转成相应的大端字节数</p>
<p>
byte[] bb = System.BitConverter.GetBytes(b); //这样直接取到的就是大端字节序字节数组。</p>
<p>
</p>
<p>
对于字符串型:使用 System.Text.Encoding.Default.GetBytes();直接取字串对应字节数组。</p>
<p>
不知道为什么这个方法取到的直接就是大端字节数组。不用转换。</p>
<p>
后来查了一下,关于字串的字节序问题,因为gbk和utf-8都是以单个字节表示数字的,所以不存在字节序问题,在多个不同系统架构都用。对于utf-16,则是以双字节表示一个整数,所以为会有字节序问题,分大小端unicode。</p>
<p>
System.Text.Encoding.Default.GetBytes();在我的简体中文系统上是以gb2312的编码,也就是单个字来进行编码的,所以也不会有字节序问题。</p>
<p>
补充:“对于任何字符编码,编码单元的顺序是由编码方案指定的,与endian无关。例如GBK的编码单元是字节,用两个字节表示一个汉字。这两个字节的顺序是固定的,不受CPU字节序的影响。UTF-16的编码单元是word(双字节),word之间的顺序是编码方案指定的,word内部的字节排列才会受到endian的影响。”,所以utf-8也没有字节序的问题。字节序问题之存在于需要使用两个字节以上来表示整数。而UTF-8只是一串字节流,不存在字节序问题,不过将这些字节流翻译成Unicode比其他的传输方式复杂。以字节为单位编码的,无论一个汉字是多少个字节,都无字节序问题。<br>
你注意,字节序问题不是指多个字节传输的先后,这个是固定的无异议的。而是指一个多字节编码在机器中的表示方式问题。<br>
char str[] = "abaksdkakskasklasflk";这个无字节序问题。<br>
但<br>
int str[] = {0x11223344, 2, 3 }就有字节序问题了。因为str同样数值不同机器中表示不同。</p>
<p>
而剩下的, 就是字符编码内部的字节序了。比如UTF-16是用两个字节表示一个字符,但是这两个字节内部如何排序,系统并不知道,所以必须指定字节序。但是UTF-8由于几个字节表示并不相同,一定要从那个表示长度的字节开始读,相当于一开始就知道该从哪里是队头队尾,所以不存在字节序问题。</p>
<p>
附上字节序说明:</p>
<p>
</p>
<p>
为什么要注意字节序的问题呢?你可能这么问。当然,如果你写的程序只在单机环境下面运行,并且不和别人的程序打交道,那么你完全可以忽略字节序的存在。但是,如果你的程序要跟别人的程序产生交互呢?尤其是当你把你在微机上运算的结果运用到计算机群上去的话。在这里我想说说两种语言。C/C++语言编写的程序里数据存储顺序是跟编译平台所在的CPU相关的,而JAVA编写的程序则唯一采用big endian方式来存储数据。试想,如果你用C/C++语言在x86平台下编写的程序跟别人的JAVA程序互通时会产生什么结果?就拿上面的 0x12345678来说,你的程序传递给别人的一个数据,将指向0x12345678的指针传给了JAVA程序,由于JAVA采取big
endian方式存储数据,很自然的它会将你的数据翻译为0x78563412。什么?竟然变成另外一个数字了?是的,就是这种后果。因此,在你的C程序传给JAVA程序之前有必要进行字节序的转换工作。</p>
<p>
</p>
<p>
用文字说明可能比较抽象,下面用图像加以说明。比如数字0x12345678在两种不同字节序CPU中的存储顺序如下所示:</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
Big Endian</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
低地址 高地址</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
-----------------------------------------></p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
| 12 | 34 | 56 | 78 |</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
Little Endian</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
低地址 高地址</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
-----------------------------------------></p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
| 78 | 56 | 34 | 12 |</p>
<div style="line-height: 14px; overflow: hidden"></div>
<p>
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+</p>
<p>
<br></p>
<p>
<br></p>
<p>
</p><div class="table-box"><table border="0" cellpadding="0" cellspacing="0" class="syntaxhighlighter csharp" style="border-spacing: 0; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; width: 674px; color: rgba(51, 51, 51, 1); border: 1px solid rgba(204, 204, 204, 1) !important; line-height: 1.1em !important; overflow: auto !important; vertical-align: baseline !important; font-size: 13px !important; background: none rgba(245, 245, 245, 1) !important"><tbody style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"><tr style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"><td class="code" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; width: 630px; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-size: 13px !important; background: none !important">
<div style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<div class="line number1 index0 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">using</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">System;</code></div>
<div class="line number2 index1 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">using</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">System.Collections.Generic;</code></div>
<div class="line number3 index2 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">using</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">System.Linq;</code></div>
<div class="line number4 index3 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">using</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">System.Text;</code></div>
<div class="line number5 index4 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">using</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">System.Net;</code></div>
<div class="line number6 index5 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
</div>
<div class="line number7 index6 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">namespace</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">ConsoleApplication2</code></div>
<div class="line number8 index7 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">{</code></div>
<div class="line number9 index8 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">class</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">Program</code></div>
<div class="line number10 index9 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">{</code></div>
<div class="line number11 index10 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">static</code> <code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">void</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">Main(</code><code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">string</code><code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">[] args)</code></div>
<div class="line number12 index11 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">{</code></div>
<div class="line number13 index12 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">byte</code><code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">[] b = { 1, 2, 3, 4 };</code></div>
<div class="line number14 index13 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code> </div>
<div class="line number15 index14 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp comments" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 130, 0, 1) !important; background: none !important">//x1是小端模式值:x1=67305985=0x04030201</code></div>
<div class="line number16 index15 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">int</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">x1 = BitConverter.ToInt32(b, 0);</code></div>
<div class="line number17 index16 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
</div>
<div class="line number18 index17 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp comments" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 130, 0, 1) !important; background: none !important">//x2是大端模式值:x2=16909060=0x01020304</code></div>
<div class="line number19 index18 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp keyword" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; font-weight: 700 !important; color: rgba(255, 120, 0, 1) !important; background: none !important">int</code> <code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">x2 = IPAddress.NetworkToHostOrder(x1);</code></div>
<div class="line number20 index19 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">}</code></div>
<div class="line number21 index20 alt2" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp spaces" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important"> </code><code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">}</code></div>
<div class="line number22 index21 alt1" style="border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; background: none !important">
<code class="csharp plain" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border: 0 !important; line-height: 1.1em !important; overflow: visible !important; vertical-align: baseline !important; color: rgba(0, 0, 0, 1) !important; background: none !important">}</code></div>
</div>
</td>
</tr></tbody></table></div><p style="color: rgba(51, 51, 51, 1); font-family: arial, "宋体", sans-serif, tahoma, "Microsoft YaHei"; font-size: 14px; line-height: 24px">
上面代码利用IPAddress.NetworkToHostOrder( )方法实现大、小端转换。</p>
<br></div>
</td>
</tr></tbody></table></div> </div>
</div><br><br>
来源:https://www.cnblogs.com/dawenxi0/p/11580420.html
頁:
[1]