C#中和Java中读取二进制数字时的不同之处
来源:互联网 发布:五行量化指标源码 编辑:程序博客网 时间:2024/06/05 00:22
今年我由c#转到JAVA写ANDROID程序,在用JAVA读我以前用C#生成的一些二进制数据文件时,发现总是出问题。后查到这里因为JAVA和C#中int 和short数据存放时高底位顺序完全不同造成的。所以不能直接读DataInputStream.readShort或DataInputStream.readInt来读取,只能一个字节一个字节的读,然后把字节顺序倒过来,再拼成short或int。
JAVA代码如下
static int readCsharpInt(InputStream in) throws IOException { byte[] bytes = new byte[4]; bytes[3] =(byte)in.read(); bytes[2] =(byte)in.read(); bytes[1] =(byte)in.read(); bytes[0] =(byte)in.read(); ByteBuffer buffer = ByteBuffer.wrap(bytes); return buffer.getInt(); } static short readCsharpShort(InputStream in) throws IOException { byte[] bytes = new byte[2]; bytes[1] =(byte)in.read(); bytes[0] =(byte)in.read(); ByteBuffer buffer = ByteBuffer.wrap(bytes); return buffer.getShort(); }
<pre name="code" class="java"> static int readJavaInt(InputStream in) throws IOException { byte[] bytes = new byte[4]; bytes[0] =(byte)in.read(); bytes[1] =(byte)in.read(); bytes[2] =(byte)in.read(); bytes[3] =(byte)in.read(); ByteBuffer buffer = ByteBuffer.wrap(bytes); return buffer.getInt(); }static short readJavaShort(InputStream in) throws IOException { byte[] bytes = new byte[2]; bytes[0] =(byte)in.read(); bytes[1] =(byte)in.read(); ByteBuffer buffer = ByteBuffer.wrap(bytes); return buffer.getShort(); }
这样就正确了,可以看到上面读取的字节排列顺序完全相反。
0 0
- C#中和Java中读取二进制数字时的不同之处
- java和C#的一些不同之处
- 统计二进制数字中1的个数
- 求一个二进制数字中1的个数?
- C#中和java不一样的参数类型
- C#&&Java-----C#中和Java中把主函数定义成静态成员函数的原因
- c中和java中数组的区别
- c中和java中数组的区别
- JAVA 与C#构造函数执行顺序的不同之处.
- 国际观点:Java与C#的十大不同之处
- java和c#语言上的一个不同之处
- Java - 输出二进制数字
- 二进制数字的乘法
- 二进制数字的表示方法
- java中抽象类与接口的不同之处
- php中session的不同之处
- mybatis 中 #{} 和 ${} 的不同之处
- C++与java的不同之处
- 将 Google Analytics(分析)添加到 Android 应用中
- daterangepicker日期选择控件
- ACL工具备份和恢复Linux的文件权限
- 游戏中广播消息公告(测试)
- hadoop2.6.4分布式配置安装
- C#中和Java中读取二进制数字时的不同之处
- /**切控件圆角 设置边框*/
- 地图与定位(七)高德定位服务
- 移动端web页面知识小结之meta部分
- ajaxFileUpload+SpringMVC框架+maven 实现文件上传
- jsp及servlet分别实现简单的计算器
- Format —— Java & Android
- A + B Problem II(高精度加法)
- elasticsearch 修改mapping