---
title: "[转载]Java 中 byte、byte 数组和 int、long 之间的转换"
author: "newboy2004"
date: "2016-05-18T10:56:17Z"
tags: []
categories: ["编程语言"]
slug: "java-byte-byte-int-long"
draft: false
lastmod: "2016-05-18T10:56:16Z"
id: 2298981
url: "https://newboy2004.iteye.com/blog/2298981"
---

[Java](http://lib.csdn.net/base/17 "Java EE知识库") 中 byte 和 int 之间的转换源码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

1. //byte 与 int 的相互转换
2. public static byte intToByte(int x) {
3. return (byte) x;
4. }
5. public static int byteToInt(byte b) {
6. //Java 总是把 byte 当做有符处理；我们可以通过将其和 0xFF 进行二进制与得到它的无符值
7. return b & 0xFF;
8. }

 

 

        测试代码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

1. //测试 int 转 byte
2. int int0 = 234;
3. byte byte0 = intToByte(int0);
4. System.out.println("byte0=" + byte0);//byte0=-22
5. //测试 byte 转 int
6. int int1 = byteToInt(byte0);
7. System.out.println("int1=" + int1);//int1=234

 

 

        Java 中 byte 数组和 int 之间的转换源码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

01. //byte 数组与 int 的相互转换
02. public static int byteArrayToInt(byte\[] b) {
03. return   b\[3] & 0xFF |
04. (b\[2] & 0xFF) &lt;&lt; 8 |
05. (b\[1] & 0xFF) &lt;&lt; 16 |
06. (b\[0] & 0xFF) &lt;&lt; 24;
07. }
08. public static byte\[] intToByteArray(int a) {
09. return new byte\[] {
10. (byte) ((a &gt;&gt; 24) & 0xFF),
11. (byte) ((a &gt;&gt; 16) & 0xFF),
12. (byte) ((a &gt;&gt; 8) & 0xFF),
13. (byte) (a & 0xFF)
14. };
15. }

 

 

        测试代码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

1. //测试 int 转 byte 数组
2. int int2 = 1417;
3. byte\[] bytesInt = intToByteArray(int2);
4. System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
5. //测试 byte 数组转 int
6. int int3 = byteArrayToInt(bytesInt);
7. System.out.println("int3=" + int3);//int3=1417

 

 

        Java 中 byte 数组和 long 之间的转换源码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

01. private static ByteBuffer buffer = ByteBuffer.allocate(8);
02. //byte 数组与 long 的相互转换
03. public static byte\[] longToBytes(long x) {
04. buffer.putLong(0, x);
05. return buffer.array();
06. }
07. public static long bytesToLong(byte\[] bytes) {
08. buffer.put(bytes, 0, bytes.length);
09. buffer.flip();//need flip
10. return buffer.getLong();
11. }

 

 

        测试代码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

1. //测试 long 转 byte 数组
2. long long1 = 2223;
3. byte\[] bytesLong = longToBytes(long1);
4. System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
5. //测试 byte 数组 转 long
6. long long2 = bytesToLong(bytesLong);
7. System.out.println("long2=" + long2);//long2=2223

 

 

        整体工具类源码：

 

**\[java]** [view plain](http://blog.csdn.net/defonds/article/details/8782785 "view plain") [copy](http://blog.csdn.net/defonds/article/details/8782785 "copy")

 

 [print](http://blog.csdn.net/defonds/article/details/8782785 "print")[?](http://blog.csdn.net/defonds/article/details/8782785 "?")

01. import java.nio.ByteBuffer;
02. public class Test {
03. private static ByteBuffer buffer = ByteBuffer.allocate(8);
04. public static void main(String\[] args) {
05. //测试 int 转 byte
06. int int0 = 234;
07. byte byte0 = intToByte(int0);
08. System.out.println("byte0=" + byte0);//byte0=-22
09. //测试 byte 转 int
10. int int1 = byteToInt(byte0);
11. System.out.println("int1=" + int1);//int1=234
12. //测试 int 转 byte 数组
13. int int2 = 1417;
14. byte\[] bytesInt = intToByteArray(int2);
15. System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
16. //测试 byte 数组转 int
17. int int3 = byteArrayToInt(bytesInt);
18. System.out.println("int3=" + int3);//int3=1417
19. //测试 long 转 byte 数组
20. long long1 = 2223;
21. byte\[] bytesLong = longToBytes(long1);
22. System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
23. //测试 byte 数组 转 long
24. long long2 = bytesToLong(bytesLong);
25. System.out.println("long2=" + long2);//long2=2223
26. }
27. //byte 与 int 的相互转换
28. public static byte intToByte(int x) {
29. return (byte) x;
30. }
31. public static int byteToInt(byte b) {
32. //Java 总是把 byte 当做有符处理；我们可以通过将其和 0xFF 进行二进制与得到它的无符值
33. return b & 0xFF;
34. }
35. //byte 数组与 int 的相互转换
36. public static int byteArrayToInt(byte\[] b) {
37. return   b\[3] & 0xFF |
38. (b\[2] & 0xFF) &lt;&lt; 8 |
39. (b\[1] & 0xFF) &lt;&lt; 16 |
40. (b\[0] & 0xFF) &lt;&lt; 24;
41. }
42. public static byte\[] intToByteArray(int a) {
43. return new byte\[] {
44. (byte) ((a &gt;&gt; 24) & 0xFF),
45. (byte) ((a &gt;&gt; 16) & 0xFF),
46. (byte) ((a &gt;&gt; 8) & 0xFF),
47. (byte) (a & 0xFF)
48. };
49. }
50. //byte 数组与 long 的相互转换
51. public static byte\[] longToBytes(long x) {
52. buffer.putLong(0, x);
53. return buffer.array();
54. }
55. public static long bytesToLong(byte\[] bytes) {
56. buffer.put(bytes, 0, bytes.length);
57. buffer.flip();//need flip
58. return buffer.getLong();
59. }
60. }

 

 

        运行测试结果：  
byte0=-22  
int1=234  
bytesInt=[B@de6ced  
int3=1417  
bytes=[B@c17164  
long2=2223  
参考文章1：[http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back](http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back)。  
参考文章2：[http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java](http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java)。  
参考文章3：[http://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java](http://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java)。

**转载自：http://blog.csdn.net/defonds/article/details/8782785**