import io.netty.buffer.ByteBuf;
import java.math.BigInteger;
public class ByteConvertUtil {
/**
* 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用
*
* @param value 要转换的int值
* @return byte数组
*/
public static byte[] intToBytes(int value) {
byte[] src = new byte[4];
src[3] = (byte) ((value >> 24) & 0xFF);
src[2] = (byte) ((value >> 16) & 0xFF);
src[1] = (byte) ((value >> 8) & 0xFF);
src[0] = (byte) (value & 0xFF);
return src;
}
/**
* 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。 和bytesToInt2()配套使用
*/
public static byte[] intToBytes2(int value) {
byte[] src = new byte[4];
src[0] = (byte) ((value >> 24) & 0xFF);
src[1] = (byte) ((value >> 16) & 0xFF);
src[2] = (byte) ((value >> 8) & 0xFF);
src[3] = (byte) (value & 0xFF);
return src;
}
/**
* byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
*
* @param src byte数组
* @param offset 从数组的第offset位开始
* @return int数值
*/
public static int bytesToInt(byte[] src, int offset) {
int value;
value = (int) ((src[offset] & 0xFF)
| ((src[offset + 1] & 0xFF) << 8)
| ((src[offset + 2] & 0xFF) << 16)
| ((src[offset + 3] & 0xFF) << 24));
return value;
}
/**
* byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
*/
public static int bytesToInt2(byte[] src, int offset) {
int value;
value = (int) (((src[offset] & 0xFF) << 24)
| ((src[offset + 1] & 0xFF) << 16)
| ((src[offset + 2] & 0xFF) << 8)
| (src[offset + 3] & 0xFF));
return value;
}
/**
* 16进制转换为字节流
*
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* 字节流转换为字符串
*
* @return
*/
public static String bytes2HexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
int first = (bytes[i] & 0xFF) >> 4;
int second = (bytes[i] & 0xFF) & 0x0F;
sb.append(int2Char(first));
sb.append(int2Char(second));
}
return sb.toString();
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
private static char int2Char(int data) {
String str = "0123456789ABCDEF";
return str.charAt(data);
}
/**
* 所有字节参与异或运算
*
* @param obj
* @return
*/
public static byte getCheckResult(byte[] bytes) {
byte result = bytes[0];
for (int i = 1; i < bytes.length; i++) {
result ^= bytes[i];
}
return result;
}
/**
* 二进制相加
*
* @param a
* @param b
* @return
*/
public static String addBinary(String a, String b) {
String result = "";
//类似于前一章的carry进位状态符
int sum = 0;
int lengthA = a.length();
int lengthB = b.length();
while (lengthA > 0 || lengthB > 0) {
if (lengthA > 0) {
//截取字符串最后一位,类似获取十进制里的个位
sum += Integer.parseInt(a.substring(lengthA - 1, lengthA));
lengthA--;
}
if (lengthB > 0) {
sum += Integer.parseInt(b.substring(lengthB - 1, lengthB));
lengthB--;
}
//当刚好满足二进制进位条件时
if (sum == 2) {
//相加刚好等于2,所以前一位剩余0,类似于十进制 4+6时,个位满十进位,个位数值为0
result = "0" + result;
//这里重新赋予1,是指进位的那一个数值,所以前面代码是用 sum+= 而不是sum=
sum = 1;
} else if (sum == 3) {
result = "1" + result;
sum = 1;
} else {
result = (sum + "") + result;
sum = 0;
}
}
//用于处理最高位进位
if (sum == 1) {
result = "00000001" + result;
}
return result;
}
/**
* 返回二进制
*
* @param udpcontent
* @return
*/
public static String getAddResultForBytes(ByteBuf udpcontent) {
udpcontent.markReaderIndex();
String jyh = new BigInteger(1, new byte[]{udpcontent.readByte()}).toString(2);
for (int i = 1; i < 63; i++) {
String tmp = new BigInteger(1, new byte[]{udpcontent.readByte()}).toString(2);
jyh = addBinary(jyh, tmp);
}
udpcontent.resetReaderIndex();
return jyh;
}
03-18
7975
