package com.pancm.Test;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class Contants {
public static void main(String[] args) throws IOException {
int a = 1233123123;
long b = 21231231;
double cc = 3233;
String ss = "abc";
byte[] bytesInt = intToByteArray(a);
byte[] bytesLong = longToByteArray(b);
byte[] bytesDouble = doubleToByteArray(cc);
double random = Math.round(Math.random() * 10000);
long long1 = new Double(random).longValue();
System.out.println(random + "=====:" + long1);
System.out.println(intToByteArray(a).length);
System.out.println(longToByteArray(b).length);
System.out.println(doubleToByteArray(cc).length);
System.out.println(stringToByteArray(ss).length);
System.out.println("==============合并解析===========");
byte[] bytesMerge = mergeBtyesprocess(bytesLong, bytesInt);
System.out.println(bytesMerge.length);
System.out.println(byteArrayToLong(subByte(bytesMerge, 0, 8)));
System.out.println(bytesMerge.length);
System.out.println(byteArrayToInt(subByte(bytesMerge, 8, 4)));
}
private static byte[] intToByteArray(int number) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeInt(number);
return bao.toByteArray();
}
private static byte[] longToByteArray(long l) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeLong(l);
return bao.toByteArray();
}
private static byte[] doubleToByteArray(double l) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeDouble(l);
System.out.println(bytes2Double(bao.toByteArray()));
return bao.toByteArray();
}
private static byte[] floatToByteArray(float l) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeFloat(l);
return bao.toByteArray();
}
private static byte[] stringToByteArray(String l) throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bao);
dos.writeBytes(l);
return bao.toByteArray();
}
private static byte[] mergeBytes(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}
/**
* 截取byte数组 不改变原数组
*
* @param b 原数组
* @param off 偏差值(索引)
* @param length 长度
* @return 截取后的数组
*/
private static byte[] subByte(byte[] b, int off, int length) {
byte[] b1 = new byte[length];
System.arraycopy(b, off, b1, 0, length);
return b1;
}
/**
* 合并byte数组
*/
private static byte[] mergeBtyesprocess(byte[] byte1, byte[] byte2) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(byte1);
os.write(byte2);
return os.toByteArray();
}
/**
* byte[]转int
*
*/
public static int byteArrayToInt(byte[] bytes) {
int value = 0;
//由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;//往高位游
}
return value;
}
public static double bytes2Double(byte[] arr) {
long value = 0;
for (int i = 0; i < 8; i++) {
value |= ((long) (arr[i] & 0xff)) << (8 * i);
}
return Double.longBitsToDouble(value);
}
public static long byteArrayToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put(bytes, 0, bytes.length);
buffer.flip();
return buffer.getLong();
}
}
07-04
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交