Skip to content

Commit 69d02eb

Browse files
Java Convert Long To Byte Array
1 parent 1e3a4f9 commit 69d02eb

File tree

15 files changed

+786
-0
lines changed

15 files changed

+786
-0
lines changed

JavaConvertLongToByteArray/.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.

JavaConvertLongToByteArray/.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.

JavaConvertLongToByteArray/.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.

JavaConvertLongToByteArray/.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.

JavaConvertLongToByteArray/.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.

JavaConvertLongToByteArray/.idea/workspace.xml

Lines changed: 584 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>

JavaConvertLongToByteArray/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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.javadeveloperzone;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.math.BigDecimal;
7+
import java.nio.Buffer;
8+
import java.nio.ByteBuffer;
9+
10+
/**
11+
* Created by JavaDeveloperZone on 2/5/2018.
12+
*/
13+
public class LongByteOperationUtil {
14+
15+
public static void main(String[] args) throws IOException {
16+
LongByteOperationUtil byteOperationUtil = new LongByteOperationUtil();
17+
long number = 4444444L;
18+
System.out.println("***************************");
19+
byte [] intBytes = byteOperationUtil.longToByteArray(number);
20+
long num = byteOperationUtil.convertByteArrayToLong(intBytes);
21+
System.out.println("Num is :"+num);
22+
23+
intBytes = byteOperationUtil.longToByteArray(number);
24+
num = byteOperationUtil.convertByteArrayToLong(intBytes);
25+
System.out.println("Num is :"+num);
26+
27+
intBytes = byteOperationUtil.convertLongArrayToByteArray(new long[]{1,2,3});
28+
long [] longArray = byteOperationUtil.convertByteArrayToLongArray(intBytes);
29+
30+
for(long l : longArray){
31+
System.out.println(l);
32+
}
33+
/*for(byte b : intBytes){
34+
System.out.println(b);
35+
}*/
36+
37+
}
38+
39+
private byte [] convertLongToByteArray(long number) {
40+
ByteBuffer byteBuffer = ByteBuffer.allocate(Long.BYTES);
41+
byteBuffer.putLong(number);
42+
return byteBuffer.array();
43+
}
44+
private static byte[] longtoBytes(long data) {
45+
return new byte[]{
46+
(byte) ((data >> 56) & 0xff),
47+
(byte) ((data >> 48) & 0xff),
48+
(byte) ((data >> 40) & 0xff),
49+
(byte) ((data >> 32) & 0xff),
50+
(byte) ((data >> 24) & 0xff),
51+
(byte) ((data >> 16) & 0xff),
52+
(byte) ((data >> 8) & 0xff),
53+
(byte) ((data >> 0) & 0xff),
54+
};
55+
}
56+
57+
private byte[] longToByteArray ( final long i ) throws IOException {
58+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
59+
DataOutputStream dos = new DataOutputStream(bos);
60+
dos.writeLong(i);
61+
dos.flush();
62+
return bos.toByteArray();
63+
}
64+
private long convertByteArrayToLong(byte[] longBytes){
65+
ByteBuffer byteBuffer = ByteBuffer.allocate(Long.BYTES);
66+
byteBuffer.put(longBytes);
67+
byteBuffer.flip();
68+
return byteBuffer.getLong();
69+
}
70+
private byte[] convertLongArrayToByteArray(long[] data) {
71+
if (data == null) return null;
72+
// ----------
73+
byte[] byts = new byte[data.length * Long.BYTES];
74+
for (int i = 0; i < data.length; i++)
75+
System.arraycopy(convertLongToByteArray(data[i]), 0, byts, i * Long.BYTES, Long.BYTES);
76+
return byts;
77+
}
78+
79+
public long[] convertByteArrayToLongArray(byte[] data) {
80+
if (data == null || data.length % Long.BYTES != 0) return null;
81+
// ----------
82+
long[] longs = new long[data.length / Long.BYTES];
83+
for (int i = 0; i < longs.length; i++)
84+
longs[i] = ( convertByteArrayToLong(new byte[] {
85+
data[(i*Long.BYTES)],
86+
data[(i*Long.BYTES)+1],
87+
data[(i*Long.BYTES)+2],
88+
data[(i*Long.BYTES)+3],
89+
data[(i*Long.BYTES)+4],
90+
data[(i*Long.BYTES)+5],
91+
data[(i*Long.BYTES)+6],
92+
data[(i*Long.BYTES)+7],
93+
} ));
94+
return longs;
95+
}
96+
97+
public long convertByteArrayToLongArrayByShift(byte[] data) {
98+
if (data == null || data.length % Long.BYTES != 0) return -1;
99+
// ----------
100+
return ( convertByteArrayToLong(new byte[] {
101+
data[(Long.BYTES)],
102+
data[(Long.BYTES)+1],
103+
data[(Long.BYTES)+2],
104+
data[(Long.BYTES)+3],
105+
data[(Long.BYTES)+4],
106+
data[(Long.BYTES)+5],
107+
data[(Long.BYTES)+6],
108+
data[(Long.BYTES)+7],
109+
} ));
110+
}
111+
112+
}
Binary file not shown.

0 commit comments

Comments
 (0)