/**
* Created by gcy on 2018/1/9 0009.
*/
public class CmdHandler {
public static byte xor(byte... bytes) {
byte result = (byte) (bytes[0] ^ bytes[1]);
for (int i = 2; i < bytes.length; ++i) {
result = (byte) (result ^ bytes[i]);
}
return result;
}
public static byte[] bytes2HexBytes(byte[] bytes) {
byte[] result = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
result[i] = (byte) (bytes[i] & 0xFF);
}
return result;
}
public static String bytes2HexString(byte[] bytes) {
String ret = "";
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += (hex.toUpperCase());
}
return ret;
}
// 位数由高到低依次作为参数传入
public static int byte2Int(byte... bytes) {
int length = bytes.length;
int result = 0;
for (int i = length; i > 0; i--) {
int temp = ((bytes[i - 1] & 0xff) << (length - i) * 8);
result = result | temp;
}
return result;
}
public static String printHexString(byte[] b) {
String a = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
a = a + hex;
}
return a;
}
public static double formatWeight(float v) {
BigDecimal decimal = new BigDecimal(String.valueOf(v));
DecimalFormat format = new DecimalFormat("####.#");
format.setRoundingMode(RoundingMode.HALF_UP);
return Double.parseDouble(format.format(decimal.doubleValue()));
}
}
CmdHandler
最新推荐文章于 2023-07-10 10:54:28 发布