java中byte数组与int,long,short间的转换

迈不过友情╰ 2022-09-17 11:28 351阅读 0赞
  1. package com.util;
  2. /**
  3. *
  4. * <ul>
  5. * <li>文件名称: com.born.util.ByteUtil.java</li>
  6. * <li>文件描述: byte转换工具</li>
  7. * <li>版权所有: 版权所有(C)2001-2006</li>
  8. * <li>公 司: bran</li>
  9. * <li>内容摘要:</li>
  10. * <li>其他说明:</li>
  11. * <li>完成日期:2011-7-18</li>
  12. * <li>修改记录0:无</li>
  13. * </ul>
  14. *
  15. * @version 1.0
  16. * @author 许力多
  17. */
  18. public class ByteUtil {
  19. /**
  20. * 转换short为byte
  21. *
  22. * @param b
  23. * @param s
  24. * 需要转换的short
  25. * @param index
  26. */
  27. public static void putShort(byte b[], short s, int index) {
  28. b[index + 1] = (byte) (s >> 8);
  29. b[index + 0] = (byte) (s >> 0);
  30. }
  31. /**
  32. * 通过byte数组取到short
  33. *
  34. * @param b
  35. * @param index
  36. * 第几位开始取
  37. * @return
  38. */
  39. public static short getShort(byte[] b, int index) {
  40. return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));
  41. }
  42. /**
  43. * 转换int为byte数组
  44. *
  45. * @param bb
  46. * @param x
  47. * @param index
  48. */
  49. public static void putInt(byte[] bb, int x, int index) {
  50. bb[index + 3] = (byte) (x >> 24);
  51. bb[index + 2] = (byte) (x >> 16);
  52. bb[index + 1] = (byte) (x >> 8);
  53. bb[index + 0] = (byte) (x >> 0);
  54. }
  55. /**
  56. * 通过byte数组取到int
  57. *
  58. * @param bb
  59. * @param index
  60. * 第几位开始
  61. * @return
  62. */
  63. public static int getInt(byte[] bb, int index) {
  64. return (int) ((((bb[index + 3] & 0xff) << 24)
  65. | ((bb[index + 2] & 0xff) << 16)
  66. | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));
  67. }
  68. /**
  69. * 转换long型为byte数组
  70. *
  71. * @param bb
  72. * @param x
  73. * @param index
  74. */
  75. public static void putLong(byte[] bb, long x, int index) {
  76. bb[index + 7] = (byte) (x >> 56);
  77. bb[index + 6] = (byte) (x >> 48);
  78. bb[index + 5] = (byte) (x >> 40);
  79. bb[index + 4] = (byte) (x >> 32);
  80. bb[index + 3] = (byte) (x >> 24);
  81. bb[index + 2] = (byte) (x >> 16);
  82. bb[index + 1] = (byte) (x >> 8);
  83. bb[index + 0] = (byte) (x >> 0);
  84. }
  85. /**
  86. * 通过byte数组取到long
  87. *
  88. * @param bb
  89. * @param index
  90. * @return
  91. */
  92. public static long getLong(byte[] bb, int index) {
  93. return ((((long) bb[index + 7] & 0xff) << 56)
  94. | (((long) bb[index + 6] & 0xff) << 48)
  95. | (((long) bb[index + 5] & 0xff) << 40)
  96. | (((long) bb[index + 4] & 0xff) << 32)
  97. | (((long) bb[index + 3] & 0xff) << 24)
  98. | (((long) bb[index + 2] & 0xff) << 16)
  99. | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
  100. }
  101. /**
  102. * 字符到字节转换
  103. *
  104. * @param ch
  105. * @return
  106. */
  107. public static void putChar(byte[] bb, char ch, int index) {
  108. int temp = (int) ch;
  109. // byte[] b = new byte[2];
  110. for (int i = 0; i < 2; i ++ ) {
  111. bb[index + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位
  112. temp = temp >> 8; // 向右移8位
  113. }
  114. }
  115. /**
  116. * 字节到字符转换
  117. *
  118. * @param b
  119. * @return
  120. */
  121. public static char getChar(byte[] b, int index) {
  122. int s = 0;
  123. if (b[index + 1] > 0)
  124. s += b[index + 1];
  125. else
  126. s += 256 + b[index + 0];
  127. s *= 256;
  128. if (b[index + 0] > 0)
  129. s += b[index + 1];
  130. else
  131. s += 256 + b[index + 0];
  132. char ch = (char) s;
  133. return ch;
  134. }
  135. /**
  136. * float转换byte
  137. *
  138. * @param bb
  139. * @param x
  140. * @param index
  141. */
  142. public static void putFloat(byte[] bb, float x, int index) {
  143. // byte[] b = new byte[4];
  144. int l = Float.floatToIntBits(x);
  145. for (int i = 0; i < 4; i++) {
  146. bb[index + i] = new Integer(l).byteValue();
  147. l = l >> 8;
  148. }
  149. }
  150. /**
  151. * 通过byte数组取得float
  152. *
  153. * @param bb
  154. * @param index
  155. * @return
  156. */
  157. public static float getFloat(byte[] b, int index) {
  158. int l;
  159. l = b[index + 0];
  160. l &= 0xff;
  161. l |= ((long) b[index + 1] << 8);
  162. l &= 0xffff;
  163. l |= ((long) b[index + 2] << 16);
  164. l &= 0xffffff;
  165. l |= ((long) b[index + 3] << 24);
  166. return Float.intBitsToFloat(l);
  167. }
  168. /**
  169. * double转换byte
  170. *
  171. * @param bb
  172. * @param x
  173. * @param index
  174. */
  175. public static void putDouble(byte[] bb, double x, int index) {
  176. // byte[] b = new byte[8];
  177. long l = Double.doubleToLongBits(x);
  178. for (int i = 0; i < 4; i++) {
  179. bb[index + i] = new Long(l).byteValue();
  180. l = l >> 8;
  181. }
  182. }
  183. /**
  184. * 通过byte数组取得float
  185. *
  186. * @param bb
  187. * @param index
  188. * @return
  189. */
  190. public static double getDouble(byte[] b, int index) {
  191. long l;
  192. l = b[0];
  193. l &= 0xff;
  194. l |= ((long) b[1] << 8);
  195. l &= 0xffff;
  196. l |= ((long) b[2] << 16);
  197. l &= 0xffffff;
  198. l |= ((long) b[3] << 24);
  199. l &= 0xffffffffl;
  200. l |= ((long) b[4] << 32);
  201. l &= 0xffffffffffl;
  202. l |= ((long) b[5] << 40);
  203. l &= 0xffffffffffffl;
  204. l |= ((long) b[6] << 48);
  205. l &= 0xffffffffffffffl;
  206. l |= ((long) b[7] << 56);
  207. return Double.longBitsToDouble(l);
  208. }
  209. }
  210. public class DataTypeChangeHelper {
  211. /**
  212. * 将一个单字节的byte转换成32位的int
  213. *
  214. * @param b
  215. * byte
  216. * @return convert result
  217. */
  218. public static int unsignedByteToInt(byte b) {
  219. return (int) b & 0xFF;
  220. }
  221. /**
  222. * 将一个单字节的Byte转换成十六进制的数
  223. *
  224. * @param b
  225. * byte
  226. * @return convert result
  227. */
  228. public static String byteToHex(byte b) {
  229. int i = b & 0xFF;
  230. return Integer.toHexString(i);
  231. }
  232. /**
  233. * 将一个4byte的数组转换成32位的int
  234. *
  235. * @param buf
  236. * bytes buffer
  237. * @param byte[]中开始转换的位置
  238. * @return convert result
  239. */
  240. public static long unsigned4BytesToInt(byte[] buf, int pos) {
  241. int firstByte = 0;
  242. int secondByte = 0;
  243. int thirdByte = 0;
  244. int fourthByte = 0;
  245. int index = pos;
  246. firstByte = (0x000000FF & ((int) buf[index]));
  247. secondByte = (0x000000FF & ((int) buf[index + 1]));
  248. thirdByte = (0x000000FF & ((int) buf[index + 2]));
  249. fourthByte = (0x000000FF & ((int) buf[index + 3]));
  250. index = index + 4;
  251. return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
  252. }
  253. /**
  254. * 将16位的short转换成byte数组
  255. *
  256. * @param s
  257. * short
  258. * @return byte[] 长度为2
  259. * */
  260. public static byte[] shortToByteArray(short s) {
  261. byte[] targets = new byte[2];
  262. for (int i = 0; i < 2; i++) {
  263. int offset = (targets.length - 1 - i) * 8;
  264. targets[i] = (byte) ((s >>> offset) & 0xff);
  265. }
  266. return targets;
  267. }
  268. /**
  269. * 将32位整数转换成长度为4的byte数组
  270. *
  271. * @param s
  272. * int
  273. * @return byte[]
  274. * */
  275. public static byte[] intToByteArray(int s) {
  276. byte[] targets = new byte[2];
  277. for (int i = 0; i < 4; i++) {
  278. int offset = (targets.length - 1 - i) * 8;
  279. targets[i] = (byte) ((s >>> offset) & 0xff);
  280. }
  281. return targets;
  282. }
  283. /**
  284. * long to byte[]
  285. *
  286. * @param s
  287. * long
  288. * @return byte[]
  289. * */
  290. public static byte[] longToByteArray(long s) {
  291. byte[] targets = new byte[2];
  292. for (int i = 0; i < 8; i++) {
  293. int offset = (targets.length - 1 - i) * 8;
  294. targets[i] = (byte) ((s >>> offset) & 0xff);
  295. }
  296. return targets;
  297. }
  298. /**32位int转byte[]*/
  299. public static byte[] int2byte(int res) {
  300. byte[] targets = new byte[4];
  301. targets[0] = (byte) (res & 0xff);// 最低位
  302. targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
  303. targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
  304. targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
  305. return targets;
  306. }
  307. /**
  308. * 将长度为2的byte数组转换为16位int
  309. *
  310. * @param res
  311. * byte[]
  312. * @return int
  313. * */
  314. public static int byte2int(byte[] res) {
  315. // res = InversionByte(res);
  316. // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
  317. int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
  318. return targets;
  319. }
  320. }

发表评论

表情:
评论列表 (有 0 条评论,351人围观)

还没有评论,来说两句吧...

相关阅读