c++和java字节高低位的转换.doc_第1页
c++和java字节高低位的转换.doc_第2页
c++和java字节高低位的转换.doc_第3页
c++和java字节高低位的转换.doc_第4页
c++和java字节高低位的转换.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论