Java数组之间的互相转换

时间:2016年2月16日13:27:33

  1. import java.io.ByteArrayInputStream;
  2. import java.io.InputStream;
  3. /**
  4. * 操作工具类
  5. */
  6. public class StreamTools {
  7. /**
  8. * @param inStream
  9. * @return 字节数组
  10. * @throws Exception
  11. * @方法功能 InputStream 转为 byte
  12. */
  13. public static byte[] inputStream2Byte(InputStream inStream)
  14. throws Exception {
  15. // ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
  16. // byte[] buffer = new byte[1024];
  17. // int len = -1;
  18. // while ((len = inStream.read(buffer)) != -1) {
  19. // outSteam.write(buffer, 0, len);
  20. // }
  21. // outSteam.close();
  22. // inStream.close();
  23. // return outSteam.toByteArray();
  24. int count = 0;
  25. while (count == 0) {
  26. count = inStream.available();
  27. }
  28. byte[] b = new byte[count];
  29. inStream.read(b);
  30. return b;
  31. }
  32. /**
  33. * @param b 字节数组
  34. * @return InputStream
  35. * @throws Exception
  36. * @方法功能 byte 转为 InputStream
  37. */
  38. public static InputStream byte2InputStream(byte[] b) throws Exception {
  39. InputStream is = new ByteArrayInputStream(b);
  40. return is;
  41. }
  42. /**
  43. * @param number 短整形
  44. * @return 两位的字节数组
  45. * @功能 短整型与字节的转换
  46. */
  47. public static byte[] shortToByte(short number) {
  48. int temp = number;
  49. byte[] b = new byte[2];
  50. for (int i = 0; i < b.length; i++) {
  51. b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
  52. temp = temp >> 8; // 向右移8位
  53. }
  54. return b;
  55. }
  56. /**
  57. * @param b 两位的字节数组
  58. * @return 短整型
  59. * @功能 字节的转换与短整型
  60. */
  61. public static short byteToShort(byte[] b) {
  62. short s = 0;
  63. short s0 = (short) (b[0] & 0xff);// 最低位
  64. short s1 = (short) (b[1] & 0xff);
  65. s1 <<= 8;
  66. s = (short) (s0 | s1);
  67. return s;
  68. }
  69. /**
  70. * @param i 整型
  71. * @return 四位的字节数组
  72. * @方法功能 整型与字节数组的转换
  73. */
  74. public static byte[] intToByte(int i) {
  75. byte[] bt = new byte[4];
  76. bt[0] = (byte) (0xff & i);
  77. bt[1] = (byte) ((0xff00 & i) >> 8);
  78. bt[2] = (byte) ((0xff0000 & i) >> 16);
  79. bt[3] = (byte) ((0xff000000 & i) >> 24);
  80. return bt;
  81. }
  82. /**
  83. * @param bytes 字节数组
  84. * @return 整型
  85. * @方法功能 字节数组和整型的转换
  86. */
  87. public static int bytesToInt(byte[] bytes) {
  88. int num = bytes[0] & 0xFF;
  89. num |= ((bytes[1] << 8) & 0xFF00);
  90. num |= ((bytes[2] << 16) & 0xFF0000);
  91. num |= ((bytes[3] << 24) & 0xFF000000);
  92. return num;
  93. }
  94. /**
  95. * @param number 字节数组
  96. * @return 长整型
  97. * @方法功能 字节数组和长整型的转换
  98. */
  99. public static byte[] longToByte(long number) {
  100. long temp = number;
  101. byte[] b = new byte[8];
  102. for (int i = 0; i < b.length; i++) {
  103. b[i] = new Long(temp & 0xff).byteValue();
  104. // 将最低位保存在最低位
  105. temp = temp >> 8;
  106. // 向右移8位
  107. }
  108. return b;
  109. }
  110. /**
  111. * @param b 字节数组
  112. * @return 长整型
  113. * @方法功能 字节数组和长整型的转换
  114. */
  115. public static long byteToLong(byte[] b) {
  116. long s = 0;
  117. long s0 = b[0] & 0xff;// 最低位
  118. long s1 = b[1] & 0xff;
  119. long s2 = b[2] & 0xff;
  120. long s3 = b[3] & 0xff;
  121. long s4 = b[4] & 0xff;// 最低位
  122. long s5 = b[5] & 0xff;
  123. long s6 = b[6] & 0xff;
  124. long s7 = b[7] & 0xff; // s0不变
  125. s1 <<= 8;
  126. s2 <<= 16;
  127. s3 <<= 24;
  128. s4 <<= 8 * 4;
  129. s5 <<= 8 * 5;
  130. s6 <<= 8 * 6;
  131. s7 <<= 8 * 7;
  132. s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
  133. return s;
  134. }
  135. /**
  136. * @param b
  137. * @说明 主要是为解析静态数据包,将一个字节数组转换为short数组
  138. */
  139. public static short[] byteArray2ShortArray(byte[] b) {
  140. int len = b.length / 2;
  141. int index = 0;
  142. short[] re = new short[len];
  143. byte[] buf = new byte[2];
  144. for (int i = 0; i < b.length; ) {
  145. buf[0] = b[i];
  146. buf[1] = b[i + 1];
  147. short st = byteToShort(buf);
  148. re[index] = st;
  149. index++;
  150. i += 2;
  151. }
  152. return re;
  153. }
  154. /**
  155. * @param b
  156. * @说明 主要是为解析静态数据包,将一个short数组反转为字节数组
  157. */
  158. public static byte[] shortArray2ByteArray(short[] b) {
  159. byte[] rebt = new byte[b.length * 2];
  160. int index = 0;
  161. for (int i = 0; i < b.length; i++) {
  162. short st = b[i];
  163. byte[] bt = shortToByte(st);
  164. rebt[index] = bt[0];
  165. rebt[index + 1] = bt[1];
  166. index += 2;
  167. }
  168. return rebt;
  169. }
  170. }






posted @ 2016-02-29 18:09  五月的雨  阅读(1000)  评论(0编辑  收藏  举报