基本类型的相互转换

Posted 再也伤不起

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基本类型的相互转换相关的知识,希望对你有一定的参考价值。

 1 public class Utilities {
 2     public static byte[] int2Bytes(int num) {
 3         byte[] byteNum = new byte[4];
 4         for (int ix = 0; ix < 4; ++ix) {
 5             int offset = 32 - (ix + 1) * 8;
 6             byteNum[ix] = (byte) ((num >> offset) & 0xff);
 7         }
 8         return byteNum;
 9     }
10 
11     public static int bytes2Int(byte[] byteNum) {
12         int num = 0;
13         for (int ix = 0; ix < 4; ++ix) {
14             num <<= 8;
15             num |= (byteNum[ix] & 0xff);
16         }
17         return num;
18     }
19 
20     public static byte int2OneByte(int num) {
21         return (byte) (num & 0x000000ff);
22     }
23 
24     public static int oneByte2Int(byte byteNum) {
25                 //针对正数的int
26         return byteNum > 0 ? byteNum : (128 + (128 + byteNum));
27     }
28 
29     public static byte[] long2Bytes(long num) {
30         byte[] byteNum = new byte[8];
31         for (int ix = 0; ix < 8; ++ix) {
32             int offset = 64 - (ix + 1) * 8;
33             byteNum[ix] = (byte) ((num >> offset) & 0xff);
34         }
35         return byteNum;
36     }
37 
38     public static long bytes2Long(byte[] byteNum) {
39         long num = 0;
40         for (int ix = 0; ix < 8; ++ix) {
41             num <<= 8;
42             num |= (byteNum[ix] & 0xff);
43         }
44         return num;
45     }
46 }
 1 public class TestMain {
 2     public static void main(String[] args) {
 3         int num = 129;
 4         System.out.println("测试的int值为:" + num);
 5 
 6         byte[] int2bytes = Utilities.int2Bytes(num);
 7         System.out.printf("int转成bytes: ");
 8         for (int i = 0; i < 4; ++i) {
 9             System.out.print(int2bytes[i] + " ");
10         }
11         System.out.println();
12 
13         int bytes2int = Utilities.bytes2Int(int2bytes);
14         System.out.println("bytes转行成int: " + bytes2int);
15 
16         byte int2OneByte = Utilities.int2OneByte(num);
17         System.out.println("int转行成one byte: " + int2OneByte);
18 
19         int oneByte2Int = Utilities.oneByte2Int(int2OneByte);
20         System.out.println("one byte转行成int: " + oneByte2Int);
21         System.out.println();
22 
23         long longNum = 100000;
24         System.out.println("测试的long值为:" + longNum);
25 
26         byte[] long2Bytes = Utilities.long2Bytes(longNum);
27         System.out.printf("long转行成bytes: ");
28         for (int ix = 0; ix < long2Bytes.length; ++ix) {
29             System.out.print(long2Bytes[ix] + " ");
30         }
31         System.out.println();
32 
33         long bytes2Long = Utilities.bytes2Long(long2Bytes);
34         System.out.println("bytes转行成long: " + bytes2Long);
35     }
36 }

测试结果:

1 测试的int值为:156
2 int转成bytes: 0 0 0 -100 
3 bytes转行成int: 156
4 int转行成one byte: -100
5 one byte转行成int: 156
6 
7 测试的long值为:100000
8 long转行成bytes: 0 0 0 0 0 1 -122 -96 
9 bytes转行成long: 100000

 

 

以上是关于基本类型的相互转换的主要内容,如果未能解决你的问题,请参考以下文章

java基本数据类型及相互间的转换

基本数据类型和String相互转换

3.6 java基础总结①包装类,基本数据类型,String相互间转换

基本数据类型包装类String类型之间的相互转换

基本类型的类型转换

包装类的基本类型与字符串类型之间的相互转换