|
@@ -0,0 +1,298 @@
|
|
|
+package com.tidecloud.dataacceptance.util;
|
|
|
+
|
|
|
+
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+public class ByteUtil {
|
|
|
+ /**
|
|
|
+ * short到字节数组的转换.
|
|
|
+ */
|
|
|
+ public static byte[] shortToByte(short number) {
|
|
|
+ int temp = number;
|
|
|
+ byte[] b = new byte[2];
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
|
|
|
+ temp = temp >> 8;// 向右移8位
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到short的转换.
|
|
|
+ */
|
|
|
+ public static short byteToShort(byte[] b) {
|
|
|
+ short s = 0;
|
|
|
+ short s0 = (short) (b[0] & 0xff);// 最低位
|
|
|
+ short s1 = (short) (b[1] & 0xff);
|
|
|
+ s1 <<= 8;
|
|
|
+ s = (short) (s0 | s1);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * int到字节数组的转换.
|
|
|
+ */
|
|
|
+ public static byte[] intToByte(int number) {
|
|
|
+ int temp = number;
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
|
|
|
+ temp = temp >> 8;// 向右移8位
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] toLH(int n) {
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ b[0] = (byte) (n & 0xff);
|
|
|
+ b[1] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[2] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[3] = (byte) (n >> 24 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static byte[] toHH(int n) {
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ b[3] = (byte) (n & 0xff);
|
|
|
+ b[2] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[1] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[0] = (byte) (n >> 24 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到int的转换.
|
|
|
+ */
|
|
|
+ public static int byteToInt(byte[] b) {
|
|
|
+ int s = 0;
|
|
|
+ int s0 = b[0] & 0xff;// 最低位
|
|
|
+ int s1 = b[1] & 0xff;
|
|
|
+ int s2 = b[2] & 0xff;
|
|
|
+ int s3 = b[3] & 0xff;
|
|
|
+ s3 <<= 24;
|
|
|
+ s2 <<= 16;
|
|
|
+ s1 <<= 8;
|
|
|
+ s = s0 | s1 | s2 | s3;
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * long类型转成byte数组
|
|
|
+ */
|
|
|
+ public static byte[] longToByte(long number) {
|
|
|
+ long temp = number;
|
|
|
+ byte[] b = new byte[8];
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp
|
|
|
+ // >> 8;// 向右移8位
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到long的转换.
|
|
|
+ */
|
|
|
+ public static long byteToLong(byte[] b) {
|
|
|
+ long s = 0;
|
|
|
+ long s0 = b[0] & 0xff;// 最低位
|
|
|
+ long s1 = b[1] & 0xff;
|
|
|
+ long s2 = b[2] & 0xff;
|
|
|
+ long s3 = b[3] & 0xff;
|
|
|
+ long s4 = b[4] & 0xff;// 最低位
|
|
|
+ long s5 = b[5] & 0xff;
|
|
|
+ long s6 = b[6] & 0xff;
|
|
|
+ long s7 = b[7] & 0xff;
|
|
|
+
|
|
|
+ // s0不变
|
|
|
+ s1 <<= 8;
|
|
|
+ s2 <<= 16;
|
|
|
+ s3 <<= 24;
|
|
|
+ s4 <<= 8 * 4;
|
|
|
+ s5 <<= 8 * 5;
|
|
|
+ s6 <<= 8 * 6;
|
|
|
+ s7 <<= 8 * 7;
|
|
|
+ s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * double到字节数组的转换.
|
|
|
+ */
|
|
|
+ public static byte[] doubleToByte(double num) {
|
|
|
+ byte[] b = new byte[8];
|
|
|
+ long l = Double.doubleToLongBits(num);
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ b[i] = new Long(l).byteValue();
|
|
|
+ l = l >> 8;
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到double的转换.
|
|
|
+ */
|
|
|
+ public static double byteToDouble(byte[] b) {
|
|
|
+ long m;
|
|
|
+ m = b[0];
|
|
|
+ m &= 0xff;
|
|
|
+ m |= ((long) b[1] << 8);
|
|
|
+ m &= 0xffff;
|
|
|
+ m |= ((long) b[2] << 16);
|
|
|
+ m &= 0xffffff;
|
|
|
+ m |= ((long) b[3] << 24);
|
|
|
+ m &= 0xffffffffL;
|
|
|
+ m |= ((long) b[4] << 32);
|
|
|
+ m &= 0xffffffffffL;
|
|
|
+ m |= ((long) b[5] << 40);
|
|
|
+ m &= 0xffffffffffffL;
|
|
|
+ m |= ((long) b[6] << 48);
|
|
|
+ m &= 0xffffffffffffffL;
|
|
|
+ m |= ((long) b[7] << 56);
|
|
|
+ return Double.longBitsToDouble(m);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * float到字节数组的转换.
|
|
|
+ */
|
|
|
+ public static byte[] floatToByte(float x) {
|
|
|
+ //先用 Float.floatToIntBits(f)转换成int
|
|
|
+ int floatToIntBits = Float.floatToIntBits(x);
|
|
|
+ return intToByte(floatToIntBits);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到float的转换.
|
|
|
+ */
|
|
|
+ public static float byteToFloat(byte[] b) {
|
|
|
+ // 4 bytes
|
|
|
+ int accum = 0;
|
|
|
+ for (int shiftBy = 0; shiftBy < 4; shiftBy++) {
|
|
|
+ accum |= (b[shiftBy] & 0xff) << shiftBy * 8;
|
|
|
+ }
|
|
|
+ return Float.intBitsToFloat(accum);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * char到字节数组的转换.
|
|
|
+ */
|
|
|
+ public static byte[] charToByte(char c) {
|
|
|
+ byte[] b = new byte[2];
|
|
|
+ b[0] = (byte) ((c & 0xFF00) >> 8);
|
|
|
+ b[1] = (byte) (c & 0xFF);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到char的转换.
|
|
|
+ */
|
|
|
+ public static char byteToChar(byte[] b) {
|
|
|
+ char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * string到字节数组的转换.
|
|
|
+ */
|
|
|
+ public static byte[] stringToByte(String str) throws UnsupportedEncodingException {
|
|
|
+ return str.getBytes("GBK");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组到String的转换.
|
|
|
+ */
|
|
|
+ public static String bytesToString(byte[] str) {
|
|
|
+ String keyword = null;
|
|
|
+ try {
|
|
|
+ keyword = new String(str, "GBK");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return keyword;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException, ClassNotFoundException {
|
|
|
+ int a = 0b00000001000000100000001100000100;
|
|
|
+ System.out.println(a);
|
|
|
+ byte[] b2 = intToByte(a);
|
|
|
+ System.out.println(Arrays.toString(b2));
|
|
|
+
|
|
|
+ int b = byteToInt(b2);
|
|
|
+ System.out.println(b);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object byteToByte(byte[] bytes) {
|
|
|
+ if (bytes.length > 0) {
|
|
|
+ return bytes[0];
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object byteToBoolean(byte[] bytes) {
|
|
|
+ if (bytes.length > 0) {
|
|
|
+ return byteToInt(bytes) > 0 ? true : false;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5",
|
|
|
+ "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
|
|
|
+
|
|
|
+ public static String byteToHexString(byte b) {
|
|
|
+ int n = b;
|
|
|
+ if (n < 0) {
|
|
|
+ n = 256 + n;
|
|
|
+ }
|
|
|
+ int d1 = n / 16;
|
|
|
+ int d2 = n % 16;
|
|
|
+ return hexDigits[d1] + hexDigits[d2];
|
|
|
+ }
|
|
|
+
|
|
|
+ //字符数组转16进制字符串
|
|
|
+ public static String byteArrayToHexString(byte[] b) {
|
|
|
+ StringBuilder resultSb = new StringBuilder();
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ resultSb.append(byteToHexString(b[i]));
|
|
|
+ }
|
|
|
+ return resultSb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ //byte数组转16进制字符串
|
|
|
+ public static String toHexString(byte[] byteArray) {
|
|
|
+ if (byteArray == null || byteArray.length < 1) {
|
|
|
+ throw new IllegalArgumentException("this byteArray must not be null or empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ final StringBuilder hexString = new StringBuilder();
|
|
|
+ for (int i = 0; i < byteArray.length; i++) {
|
|
|
+ if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
|
|
|
+ {
|
|
|
+ hexString.append("0");
|
|
|
+ }
|
|
|
+ hexString.append(Integer.toHexString(0xFF & byteArray[i]));
|
|
|
+ }
|
|
|
+ return hexString.toString().toLowerCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ //16进制字符串转数组
|
|
|
+ public static byte[] toByteArray(String hexString) {
|
|
|
+ if (StringUtils.isEmpty(hexString)) {
|
|
|
+ throw new IllegalArgumentException("this hexString must not be empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ hexString = hexString.toLowerCase();
|
|
|
+ final byte[] byteArray = new byte[hexString.length() / 2];
|
|
|
+ int k = 0;
|
|
|
+ for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
|
|
|
+ byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
|
|
|
+ byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
|
|
|
+ byteArray[i] = (byte) (high << 4 | low);
|
|
|
+ k += 2;
|
|
|
+ }
|
|
|
+ return byteArray;
|
|
|
+ }
|
|
|
+}
|