Skip to content

Commit b85d5bf

Browse files
Java Byte Operation
1 parent 2ade3b8 commit b85d5bf

18 files changed

+961
-0
lines changed

JavaByteOperation/.idea/compiler.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaByteOperation/.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaByteOperation/.idea/kotlinc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaByteOperation/.idea/misc.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaByteOperation/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaByteOperation/.idea/workspace.xml

Lines changed: 555 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>

JavaByteOperation/abc.txt

Whitespace-only changes.

JavaByteOperation/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>JavaByteOperation</groupId>
8+
<artifactId>JavaByteOperation</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
</properties>
14+
<build>
15+
<finalName>JavaByteOperation</finalName>
16+
<plugins>
17+
<plugin>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<configuration>
20+
<source>1.8</source>
21+
<target>1.8</target>
22+
</configuration>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
27+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.javadeveloperzone;
2+
3+
import java.nio.ByteBuffer;
4+
5+
/**
6+
* Created by JavaDeveloperZone on 2/5/2018.
7+
*/
8+
public class ByteOperationUtil {
9+
10+
public static void main(String[] args) {
11+
ByteOperationUtil byteOperationUtil = new ByteOperationUtil();
12+
int number = 444;
13+
System.out.println("***************************");
14+
byte [] intBytes = byteOperationUtil.convertIntToByteArray(number);
15+
int num = byteOperationUtil.convertByteArrayToInt(intBytes);
16+
System.out.println("Num is :"+num);
17+
for(byte b : intBytes){
18+
System.out.println(b);
19+
}
20+
21+
System.out.println("***************************");
22+
float floatNumber = 444.4f;
23+
intBytes = byteOperationUtil.convertFloatToByteArray(floatNumber);
24+
floatNumber = byteOperationUtil.convertByteArrayToFlat(intBytes);
25+
System.out.println("Num is :"+floatNumber);
26+
for(byte b : intBytes){
27+
System.out.println(b);
28+
}
29+
System.out.println("***************************");
30+
double doubleNumber = 888.4d;
31+
intBytes = byteOperationUtil.convertDoubleToByteArray(doubleNumber);
32+
doubleNumber = byteOperationUtil.convertByteArrayToDouble(intBytes);
33+
System.out.println("Num is :"+doubleNumber);
34+
for(byte b : intBytes){
35+
System.out.println(b);
36+
}
37+
38+
}
39+
40+
private byte [] convertIntToByteArray(int number) {
41+
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
42+
byteBuffer.putInt(number);
43+
return byteBuffer.array();
44+
}
45+
46+
private byte [] convertDoubleToByteArray(double number) {
47+
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
48+
byteBuffer.putDouble(number);
49+
return byteBuffer.array();
50+
}
51+
private byte [] convertFloatToByteArray(float number) {
52+
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
53+
byteBuffer.putFloat(number);
54+
return byteBuffer.array();
55+
}
56+
57+
private int convertByteArrayToInt(byte[] intBytes){
58+
ByteBuffer byteBuffer = ByteBuffer.wrap(intBytes);
59+
return byteBuffer.getInt();
60+
}
61+
private byte[] convertIntArrayToByteArray(int[] data) {
62+
if (data == null) return null;
63+
// ----------
64+
byte[] byts = new byte[data.length * 4];
65+
for (int i = 0; i < data.length; i++)
66+
System.arraycopy(convertIntToByteArray(data[i]), 0, byts, i * 4, 4);
67+
return byts;
68+
}
69+
70+
public int[] convertByteArrayToIntArray(byte[] data) {
71+
if (data == null || data.length % 4 != 0) return null;
72+
// ----------
73+
int[] ints = new int[data.length / 4];
74+
for (int i = 0; i < ints.length; i++)
75+
ints[i] = ( convertByteArrayToInt(new byte[] {
76+
data[(i*4)],
77+
data[(i*4)+1],
78+
data[(i*4)+2],
79+
data[(i*4)+3],
80+
} ));
81+
return ints;
82+
}
83+
84+
private double convertByteArrayToDouble(byte[] doubleBytes){
85+
ByteBuffer byteBuffer = ByteBuffer.wrap(doubleBytes);
86+
return byteBuffer.getDouble();
87+
}
88+
private float convertByteArrayToFlat(byte[] floatBytes){
89+
ByteBuffer byteBuffer = ByteBuffer.wrap(floatBytes);
90+
return byteBuffer.getFloat();
91+
}
92+
}

0 commit comments

Comments
 (0)