c#字节数组与int转换(含高低位转换的内容)

上传人:xiao****1972 文档编号:84136278 上传时间:2019-03-02 格式:DOCX 页数:2 大小:20.24KB
返回 下载 相关 举报
c#字节数组与int转换(含高低位转换的内容)_第1页
第1页 / 共2页
c#字节数组与int转换(含高低位转换的内容)_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《c#字节数组与int转换(含高低位转换的内容)》由会员分享,可在线阅读,更多相关《c#字节数组与int转换(含高低位转换的内容)(2页珍藏版)》请在金锄头文库上搜索。

1、字节数组与int转换 在C#中将INT型转为字节数组后,其是以高位到低位排序存储的,而在C+和JAVA中是以低位到高位排序的,以致如果直接将转换后的字节数组与C+或JAVA通信时会出错。需要反排序后再传输。字节转为Int代码C#转换代码如下:C#byte bytes = 0, 0, 0, 25 ;/ If the system architecture is little-endian (that is, little end first),/ reverse the byte array.if (BitConverter.IsLittleEndian) /判断计算机结构的 endian 设置

2、 Array.Reverse(bytes); /转换排序int i = BitConverter.ToInt32(bytes, 0);Console.WriteLine(int: 0, i);/ Output: int: 25BitConverter.IsLittleEndian 字段为指示数据在此计算机结构中存储时的字节顺序(“Endian”性质)。如果结构为 Little-endian,则该值为 true;如果结构为 Big-endian,则该值为 false。不同的计算机结构采用不同的字节顺序存储数据。“Big-endian”表示最大的有效字节位于单词的左端。“Little-endian

3、”表示最大的有效字节位于单词的右端。Int转为字节代码 C#转换代码如下: byte aa = BitConverter.GetBytes(1243); if (BitConverter.IsLittleEndian)Array.Reverse(aa);JAVA转换代码如下:public byte int2bytes(int a, boolean isHighFirst) byte result = new byte4; if (isHighFirst) result0 = (byte)(a 24 & 0xff); result1 = (byte)(a 16 & 0xff); result2 = (byte)(a 8 & 0xff); result3 = (byte)(a & 0xff); else result3 = (byte)(a 24 & 0xff); result2 = (byte)(a 16 & 0xff); result1 = (byte)(a 8 & 0xff); result0 = (byte)(a & 0xff); return result;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号