forked from nsquare-jdzone/java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteOperationUtil.java
More file actions
92 lines (82 loc) · 3.18 KB
/
ByteOperationUtil.java
File metadata and controls
92 lines (82 loc) · 3.18 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.javadeveloperzone;
import java.nio.ByteBuffer;
/**
* Created by JavaDeveloperZone on 2/5/2018.
*/
public class ByteOperationUtil {
public static void main(String[] args) {
ByteOperationUtil byteOperationUtil = new ByteOperationUtil();
int number = 444;
System.out.println("***************************");
byte [] intBytes = byteOperationUtil.convertIntToByteArray(number);
int num = byteOperationUtil.convertByteArrayToInt(intBytes);
System.out.println("Num is :"+num);
for(byte b : intBytes){
System.out.println(b);
}
System.out.println("***************************");
float floatNumber = 444.4f;
intBytes = byteOperationUtil.convertFloatToByteArray(floatNumber);
floatNumber = byteOperationUtil.convertByteArrayToFlat(intBytes);
System.out.println("Num is :"+floatNumber);
for(byte b : intBytes){
System.out.println(b);
}
System.out.println("***************************");
double doubleNumber = 888.4d;
intBytes = byteOperationUtil.convertDoubleToByteArray(doubleNumber);
doubleNumber = byteOperationUtil.convertByteArrayToDouble(intBytes);
System.out.println("Num is :"+doubleNumber);
for(byte b : intBytes){
System.out.println(b);
}
}
private byte [] convertIntToByteArray(int number) {
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.putInt(number);
return byteBuffer.array();
}
private byte [] convertDoubleToByteArray(double number) {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.putDouble(number);
return byteBuffer.array();
}
private byte [] convertFloatToByteArray(float number) {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.putFloat(number);
return byteBuffer.array();
}
private int convertByteArrayToInt(byte[] intBytes){
ByteBuffer byteBuffer = ByteBuffer.wrap(intBytes);
return byteBuffer.getInt();
}
private byte[] convertIntArrayToByteArray(int[] data) {
if (data == null) return null;
// ----------
byte[] byts = new byte[data.length * 4];
for (int i = 0; i < data.length; i++)
System.arraycopy(convertIntToByteArray(data[i]), 0, byts, i * 4, 4);
return byts;
}
public int[] convertByteArrayToIntArray(byte[] data) {
if (data == null || data.length % 4 != 0) return null;
// ----------
int[] ints = new int[data.length / 4];
for (int i = 0; i < ints.length; i++)
ints[i] = ( convertByteArrayToInt(new byte[] {
data[(i*4)],
data[(i*4)+1],
data[(i*4)+2],
data[(i*4)+3],
} ));
return ints;
}
private double convertByteArrayToDouble(byte[] doubleBytes){
ByteBuffer byteBuffer = ByteBuffer.wrap(doubleBytes);
return byteBuffer.getDouble();
}
private float convertByteArrayToFlat(byte[] floatBytes){
ByteBuffer byteBuffer = ByteBuffer.wrap(floatBytes);
return byteBuffer.getFloat();
}
}