java byte 與 binary 轉換

  1. 在寫通訊程序時,有時需要解析二進制數據流,可能會遇到java數值類型與二進制byte數組的類型轉換問題。因此,筆者提供下面代碼實例,供大家需要時參考。  
  2. import java.io.*;  
  3. /** 
  4.  * java數值類型與二進制類型相互轉換 
  5.  * @author lijun 
  6.  * 
  7.  */  
  8. public class MyTest {  
  9. /** 
  10. * int32轉換爲二進制(4個字節) 
  11. * @param i 待轉換的整數 
  12. * @return 返回4字節二進制數 
  13. */  
  14. public byte[] int2byte(int i){  
  15. byte[] res = new byte[4];  
  16. res[3] = (byte)i;  
  17. res[2] = (byte)(i>>>8);  
  18. res[1] = (byte)(i>>>16);  
  19. res[0] = (byte)(i>>>24);  
  20. return res;  
  21. }  
  22. /** 
  23. * 4字節二進制數轉換爲int32的整數 
  24. * @param bytes 4字節的二進制數 
  25. * @return int32整數 
  26. */  
  27. public int byte2int(byte[] bytes){  
  28. int res = (((bytes[0]<<24)>>>24)<<24)|(((bytes[1]<<24)>>>24)<<16)|(((bytes[2]<<24)>>>24)<<8)|((bytes[3]<<24)>>>24);  
  29. return res;  
  30. }  
  31. /** 
  32. * int16整數轉換二2字節的二進制數 
  33. * @param i int16整數 
  34. * @return 2字節的二進制數 
  35. */  
  36. public byte[] int16Tobyte(int i){  
  37. byte[] res = new byte[2];  
  38. res[1] = (byte)i;  
  39. res[0] = (byte)(i>>>8);  
  40. return res;  
  41. }  
  42. /** 
  43. * 2字節的二進制數轉換爲int16整數 
  44. * @param bytes 2字節的二進制數 
  45. * @return int16整數 
  46. */  
  47. public int byteToint16(byte[] bytes){  
  48. int res = ((bytes[0]<<8)|((bytes[1]<<24)>>>24));  
  49. return res;  
  50. }  
  51. /** 
  52. * 長整型long轉換爲8字節的二進制數 
  53. * @param l 長整型long 
  54. * @return 8字節的二進制數 
  55. */  
  56. public byte[] long2byte(long l){  
  57. byte[] res = new byte[8];  
  58. res[7] = (byte)l;  
  59. res[6] = (byte)(l>>>8);  
  60. res[5] = (byte)(l>>>16);  
  61. res[4] = (byte)(l>>>24);  
  62. res[3] = (byte)(l>>>32);  
  63. res[2] = (byte)(l>>>40);  
  64. res[1] = (byte)(l>>>48);  
  65. res[0] = (byte)(l>>>56);  
  66. return res;  
  67. }  
  68. /** 
  69. * 8字節的二進制數轉換爲長整型long 
  70. * @param bytes 8字節的二進制數 
  71. * @return 長整型long 
  72. */  
  73. public long byte2long(byte[] bytes){  
  74. long l0 = bytes[0];  
  75. long l1 = bytes[1];  
  76. long l2 = bytes[2];  
  77. long l3 = bytes[3];  
  78. long l4 = bytes[4];  
  79. long l5 = bytes[5];  
  80. long l6 = bytes[6];  
  81. long l7 = bytes[7];  
  82. long res = (l0<<56)|(((l1<<56)>>>56)<<48)|(((l2<<56)>>>56)<<40)|(((l3<<56)>>>56)<<32)|(((l4<<56)>>>56)<<24)|(((l5<<56)>>>56)<<16)|(((l6<<56)>>>56)<<8)|((l7<<56)>>>56);  
  83. return res;  
  84. }  
  85. /** 
  86. * 浮點型float轉換爲4字節的二進制數 
  87. * @param f 浮點數float 
  88. * @return 4字節的二進制數 
  89. */  
  90. public byte[] float2byte(float f){  
  91. byte[] res = new byte[4];  
  92. int l = Float.floatToIntBits(f);  
  93. for(int i=3; i>=0; i--){  
  94. res[i] = new Integer(l).byteValue();  
  95. l >>= 8;  
  96. }  
  97. return res;  
  98. }  
  99. /** 
  100. * 4字節的二進制數轉換爲浮點數float 
  101. * @param bytes 4字節的二進制數 
  102. * @return 浮點數float 
  103. */  
  104. public float byte2float(byte[] bytes){  
  105. int l = byte2int(bytes);  
  106. float res = Float.intBitsToFloat(l);  
  107. return res;  
  108. }  
  109. /** 
  110. * 雙浮點數轉換爲8字節的二進制數 
  111. * @param d 雙浮點數double 
  112. * @return 8字節的二進制數 
  113. */  
  114. public byte[] double2byte(double d){  
  115. long l = Double.doubleToLongBits(d);  
  116. byte[] res = long2byte(l);  
  117. return res;  
  118. }  
  119. /** 
  120. * 8字節的二進制數轉換爲雙浮點數 
  121. * @param bytes 8字節的二進制數 
  122. * @return 雙浮點數double 
  123. */  
  124. public double byte2double(byte[] bytes){  
  125. long l = byte2long(bytes);  
  126. double res = Double.longBitsToDouble(l);  
  127. return res;  
  128. }  
  129. public void test()throws Exception{  
  130. FileOutputStream fos = new FileOutputStream("d:/data.dat");  
  131. java.io.DataOutputStream dos = new DataOutputStream(fos);  
  132. double d = 233344.233444;  
  133. dos.writeDouble(d);  
  134. dos.close();  
  135. fos.close();  
  136. }  
  137. public static void main(String[] args)throws Exception{  
  138. System.out.println("begin write a int type to file:'c:/data.dat'");  
  139. new MyTest().test();  
  140. System.out.println("end write..");  
  141. }  
  142. }  
  143.   
  144. 以下爲單元測試代碼:  
  145. import junit.framework.Assert;  
  146.   
  147.   
  148. import org.junit.Test;  
  149.   
  150.   
  151.   
  152.   
  153. public class MyTestTest {  
  154.    private MyTest test = new MyTest();  
  155. @Test  
  156. public void int2byteTest()  
  157. {  
  158. int i = -10101;  
  159. byte[] bytes = test.int2byte(i);  
  160. int j = test.byte2int(bytes);  
  161.   
  162. Assert.assertEquals(i, j);  
  163. i = 10101;  
  164. bytes = test.int2byte(i);  
  165. j = test.byte2int(bytes);  
  166. Assert.assertEquals(i, j);  
  167. i = -0x1f1f1f;  
  168. bytes = test.int2byte(i);  
  169. j = test.byte2int(bytes);  
  170. Assert.assertEquals(i, j);  
  171. i = 0x1f1f1f;  
  172. bytes = test.int2byte(i);  
  173. j = test.byte2int(bytes);  
  174. Assert.assertEquals(i, j);  
  175. i = 0x1f1f1f1f;  
  176. bytes = test.int2byte(i);  
  177. j = test.byte2int(bytes);  
  178. Assert.assertEquals(i, j);  
  179. i = -0x1f1f1f1f;  
  180. bytes = test.int2byte(i);  
  181. j = test.byte2int(bytes);  
  182. Assert.assertEquals(i, j);  
  183. }  
  184.   
  185. @Test  
  186. public void int16TobyteTest(){  
  187. int i = -0x01;  
  188. byte[] bytes = test.int16Tobyte(i);  
  189. int j = test.byteToint16(bytes);  
  190. Assert.assertEquals("-0x01",i, j);  
  191. i = 0x01;  
  192. bytes = test.int16Tobyte(i);  
  193. j = test.byteToint16(bytes);  
  194. Assert.assertEquals("0x01",i, j);  
  195. i = -0x0101;  
  196. bytes = test.int16Tobyte(i);  
  197. j = test.byteToint16(bytes);  
  198. Assert.assertEquals("-0x0101",i, j);  
  199. i = 0x0101;  
  200. bytes = test.int16Tobyte(i);  
  201. j = test.byteToint16(bytes);  
  202. Assert.assertEquals("0x0101",i, j);  
  203. }  
  204.   
  205. @Test  
  206. public void long2byteTest(){  
  207.   long l = -0x01;  
  208.   byte[] bytes = test.long2byte(l);  
  209.   long j = test.byte2long(bytes);  
  210.   Assert.assertEquals("-0x01",l, j);  
  211.   l = 0x01;  
  212.   bytes = test.long2byte(l);  
  213.   j = test.byte2long(bytes);  
  214.   Assert.assertEquals("0x01",l, j);  
  215.   l = -0x0101;  
  216.   bytes = test.long2byte(l);  
  217.   j = test.byte2long(bytes);  
  218.   Assert.assertEquals("-0x0101",l, j);  
  219.   l = -0x0101;  
  220.   bytes = test.long2byte(l);  
  221.   j = test.byte2long(bytes);  
  222.   Assert.assertEquals("-0x0101",l, j);  
  223. }  
  224.   
  225. @Test  
  226. public void float2byteTest()  
  227. {  
  228. float f = 233344.233444f;  
  229. byte[] bytes = test.float2byte(f);  
  230. float j = test.byte2float(bytes);  
  231. Assert.assertEquals(f, j);  
  232. }  
  233.   
  234. @Test  
  235. public void double2byteTest(){  
  236. double d = 233344.233444;  
  237. byte[] bytes = test.double2byte(d);  
  238. double j = test.byte2double(bytes);  
  239. Assert.assertEquals(d, j);  
  240. }  
  241. }  
發佈了79 篇原創文章 · 獲贊 193 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章