?
1
2
3
4
5
6
7
8
9
|
//byte 与 int 的相互转换
public static byte intToByte( int x) {
return ( byte ) x;
}
public static int byteToInt( byte b) {
//Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
return b & 0xFF ;
}
|
测试代码:
?
1
2
3
4
5
6
7
|
//测试 int 转 byte
int int0 = 234 ;
byte byte0 = intToByte(int0);
System.out.println( "byte0=" + byte0); //byte0=-22
//测试 byte 转 int
int int1 = byteToInt(byte0);
System.out.println( "int1=" + int1); //int1=234
|
二、Java 中 byte 数组和 int 之间的转换源码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//byte 数组与 int 的相互转换
public static int byteArrayToInt( byte [] b) {
return b[ 3 ] & 0xFF |
(b[ 2 ] & 0xFF ) << 8 |
(b[ 1 ] & 0xFF ) << 16 |
(b[ 0 ] & 0xFF ) << 24 ;
}
public static byte [] intToByteArray( int a) {
return new byte [] {
( byte ) ((a >> 24 ) & 0xFF ),
( byte ) ((a >> 16 ) & 0xFF ),
( byte ) ((a >> 8 ) & 0xFF ),
( byte ) (a & 0xFF )
};
}
|
测试代码:
?
1
2
3
4
5
6
7
|
//测试 int 转 byte 数组
int int2 = 1417 ;
byte [] bytesInt = intToByteArray(int2);
System.out.println( "bytesInt=" + bytesInt); //bytesInt=[B@de6ced
//测试 byte 数组转 int
int int3 = byteArrayToInt(bytesInt);
System.out.println( "int3=" + int3); //int3=1417
|
三、Java 中 byte 数组和 long 之间的转换源码:
?
1
2
3
4
5
6
7
8
9
10
11
12
|
private static ByteBuffer buffer = ByteBuffer.allocate( 8 );
//byte 数组与 long 的相互转换
public static byte [] longToBytes( long x) {
buffer.putLong( 0 , x);
return buffer.array();
}
public static long bytesToLong( byte [] bytes) {
buffer.put(bytes, 0 , bytes.length);
buffer.flip(); //need flip
return buffer.getLong();
}
|
测试代码:
?
1
2
3
4
5
6
7
|
//测试 long 转 byte 数组
long long1 = 2223 ;
byte [] bytesLong = longToBytes(long1);
System.out.println( "bytes=" + bytesLong); //bytes=[B@c17164
//测试 byte 数组 转 long
long long2 = bytesToLong(bytesLong);
System.out.println( "long2=" + long2); //long2=2223
|
四、整体工具类源码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import java.nio.ByteBuffer;
public class Test {
private static ByteBuffer buffer = ByteBuffer.allocate( 8 );
public static void main(String[] args) {
//测试 int 转 byte
int int0 = 234 ;
byte byte0 = intToByte(int0);
System.out.println( "byte0=" + byte0); //byte0=-22
//测试 byte 转 int
int int1 = byteToInt(byte0);
System.out.println( "int1=" + int1); //int1=234
//测试 int 转 byte 数组
int int2 = 1417 ;
byte [] bytesInt = intToByteArray(int2);
System.out.println( "bytesInt=" + bytesInt); //bytesInt=[B@de6ced
//测试 byte 数组转 int
int int3 = byteArrayToInt(bytesInt);
System.out.println( "int3=" + int3); //int3=1417
//测试 long 转 byte 数组
long long1 = 2223 ;
byte [] bytesLong = longToBytes(long1);
System.out.println( "bytes=" + bytesLong); //bytes=[B@c17164
//测试 byte 数组 转 long
long long2 = bytesToLong(bytesLong);
System.out.println( "long2=" + long2); //long2=2223
}
//byte 与 int 的相互转换
public static byte intToByte( int x) {
return ( byte ) x;
}
public static int byteToInt( byte b) {
//Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
return b & 0xFF ;
}
//byte 数组与 int 的相互转换
public static int byteArrayToInt( byte [] b) {
return b[ 3 ] & 0xFF |
(b[ 2 ] & 0xFF ) << 8 |
(b[ 1 ] & 0xFF ) << 16 |
(b[ 0 ] & 0xFF ) << 24 ;
}
public static byte [] intToByteArray( int a) {
return new byte [] {
( byte ) ((a >> 24 ) & 0xFF ),
( byte ) ((a >> 16 ) & 0xFF ),
( byte ) ((a >> 8 ) & 0xFF ),
( byte ) (a & 0xFF )
};
}
//byte 数组与 long 的相互转换
public static byte [] longToBytes( long x) {
buffer.putLong( 0 , x);
return buffer.array();
}
public static long bytesToLong( byte [] bytes) {
buffer.put(bytes, 0 , bytes.length);
buffer.flip(); //need flip
return buffer.getLong();
}
}
|
运行测试结果:
?
1
2
3
4
5
6
|
byte0=-22
int1=234
bytesInt=[B@de6ced
int3=1417
bytes=[B@c17164
long2=2223
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 72
-
2025-05-29 29
-
2025-05-25 19
-
2025-05-25 31
-
2025-05-25 79
热门评论