全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
c+和java字节高低位的转换现在我们需要从一个C/C+语言生成的二进制文件中读出一个float数据1. /参见java.io.DataInputStream2. /C+写入的字节顺序是从低到高(左低到右高),3. 而java.io.DataInputStream读取的数据是从高到低(左高到右低)4. /所以需要自己改写一下5. /功能和java.io.DataInputStream类似的6. publicclassCppInputStreamextendsFilterInputStream7. 8. publicCppInputStream(InputStreamin)9. super(in);10. 11. publicfinalintread(byteb)throwsIOException12. returnin.read(b,0,b.length);13. 14. 15. publicfinalintread(byteb,intoff,intlen)throwsIOException16. returnin.read(b,off,len);17. 18. 19. publicfinalvoidreadFully(byteb)throwsIOException20. readFully(b,0,b.length);21. 22. 23. publicfinalvoidreadFully(byteb,intoff,intlen)throwsIOException24. if(len0)25. thrownewIndexOutOfBoundsException();26. intn=0;27. while(nlen)28. intcount=in.read(b,off+n,len-n);29. if(count0)30. thrownewEOFException();31. n+=count;32. 33. 34. 35. publicfinalintskipBytes(intn)throwsIOException36. inttotal=0;37. intcur=0;38. while(total0)39. total+=cur;40. 41. returntotal;42. 43. 44. publicfinalbytereadByte()throwsIOException45. intch=in.read();46. if(ch0)47. thrownewEOFException();48. return(byte)(ch);49. 50. 51. publicfinalintreadUnsignedByte()throwsIOException52. intch=in.read();53. if(ch0)54. thrownewEOFException();55. returnch;56. 57. 58. publicfinalshortreadShort()throwsIOException59. intch2=in.read();60. intch1=in.read();61. if(ch1|ch2)0)62. thrownewEOFException();63. return(short)(ch18)+(ch20);64. 65. 66. publicfinalintreadUnsignedShort()throwsIOException67. intch2=in.read();68. intch1=in.read();69. if(ch1|ch2)0)70. thrownewEOFException();71. return(ch18)+(ch20);72. 73. 74. publicfinalcharreadChar()throwsIOException75. intch2=in.read();76. intch1=in.read();77. if(ch1|ch2)0)78. thrownewEOFException();79. return(char)(ch18)+(ch20);80. 81. 82. publicfinalintreadInt()throwsIOException83. intch4=in.read();84. intch3=in.read();85. intch2=in.read();86. intch1=in.read();87. if(ch1|ch2|ch3|ch4)0)88. thrownewEOFException();89. return(ch124)+(ch216)+(ch38)+(ch40);90. 91. 92. privatebytereadBuffer=newbyte8;93. 94. publicfinallongreadLong()throwsIOException95. readFully(readBuffer,0,8);96. return(long)readBuffer756)+(long)(readBuffer6&255)48)97. +(long)(readBuffer5&255)40)+(long)(readBuffer4&255)32)98. +(long)(readBuffer3&255)24)+(readBuffer2&255)16)99. +(readBuffer1&255)8)+(readBuffer0&255)0);100. 101. 102. publicfinalfloatreadFloat()throwsIOException103. returnFBitsToFloat(readInt();104. 105. 106. publicfinaldoublereadDouble()throwsIOException107. returnDouble.longBitsToDouble(readLong();108. 109. yy629(资深程序员) 2010-05-12简单呀,用Java的字节流,读取一个int的4个字节,然后转换。比如读取的数据是:byte1 byte2 byte3 byte4在其实这是C+的 byte4 byte3 byte2 byte1那你可以用位运算转成Java中的对应的整数:(byte1& 0xff)0 + (byte2& 0xff)8 + (byte3& 0xff) 16 +(byte4& 0xff)8);7. bytebyte2=(byte)(javaReadInt&0xff0000)16);8. bytebyte1=(byte)(javaReadInt&0xff000000)24);9. 10. /拼装成正确的int11. intrealint=(byte1&0xff)0+(byte2&0xff)8+(byte3&0xff)16+(byte4&0xff)24;另外可以使用ByteBuffer来完成,而不需要自己考虑,如何将字节数组转换为其他数据类型. 使用ByteBuffer,可以设置字节顺序.ByteBuffer简单的例子import java.nio.ByteBuffer;import java.nio.ByteOrder;public class ByteBufferTest public static void main(String args) /将字节数组转换为int类型 byte bytes = 0,0,0,1; ByteBuffer buffer = ByteBuffer.wrap
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高效能冷链物流建设方案
- 南宁理工学院《设计心理学》2025-2026学年第一学期期末试卷
- 防城港职业技术学院《有机化学(一)》2025-2026学年第一学期期末试卷
- 四川省第七地质大队招聘笔试真题2024
- 内江卫生与健康职业学院《言语与语言障碍儿童心理与教育》2025-2026学年第一学期期末试卷
- 首都医科大学附属北京朝阳医院招聘考试真题2024
- 十堰市消防员考试题库2025
- 华东师范大学《营销研究方法论》2025-2026学年第一学期期末试卷
- 衡水学院《动力机械原理》2025-2026学年第一学期期末试卷
- 商业保理客户经理客户经理技能竞赛方案
- 达州市2025届高三(一诊)第一次诊断性测试数学试卷
- 【MOOC】货币金融学-西南财经大学 中国大学慕课MOOC答案
- CAD教程-AutoCAD2024全套教程
- 线路工程竣工预验收(投运前)监理初检报告模板
- 第三单元 商业贸易与日常生活 课件高三历史统编版选择性必修2一轮复习
- 保安公司进场方案(2篇)
- 伤口造口专科护士进修汇报
- 2024年浙江首考高考英语卷试题真题及答案解析(含听力原文+作文范文)
- 《压力性损伤的预防和治疗:临床实践指南》更新解读
- 物流包装材料课件
- 第二章 田径-跨栏跑 说课课件 2023-2024学年人教版初中体育与健康九年级全一册
评论
0/150
提交评论