看到我正在处理一些硬件交互,当时我在 util.xml 中编写了不同的方法。它也有助于所有不同的转化。
private static String binaryToHex(String bin) {
return String.format("%X", Long.parseLong(bin,2)) ;
}
public static String hexToBinary16Bit(String hex16bit) {
int i = Integer.parseInt(hex16bit, 16);
String bin = toBinary(i);
return String.format("%0d", Integer.parseInt(bin));
}
private static String hexToBinary(String hex) {
int i = Integer.parseInt(hex, 16);
String bin = Integer.toBinaryString(i);
return String.format("%08d", Integer.parseInt(bin));
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
private static String hexBin(String hex) {
int a = Integer.parseInt(hex, 16);
System.out.println(a);
String finalBin = null;
int bit=1;
for(int i=0; i<16; i++) {
sBuilder.append((((a&bit)==0)?0:1));
finalBin = sBuilder.toString();
bit*=2;
}
return finalBin;
}
public static String[] customSplit(String src) {
String [] output = src.replaceAll("\\[","").replaceAll(" \\]","").split(",");
for (String element : output) {
System.out.println(element);
}
return output;
}
private static String hexToASCII(String hexValue) {
StringBuilder output = new StringBuilder("");
for (int i = 0; i < hexValue.length(); i += 2) {
String str = hexValue.substring(i, i + 2);
output.append((char) Integer.parseInt(str, 16));
}
return output.toString();
}
public static String hexBin8bit(String hex) {
StringBuilder sBuilder = new StringBuilder();
int a = Integer.parseInt(hex, 16);
System.out.println(a);
String finalBin = null;
int bit=1;
for(int i=0; i<8; i++) {
sBuilder.append((((a&bit)==0)?0:1));
finalBin = sBuilder.toString();
bit*=2;
}
return finalBin;
}
public static int toInt32(byte[] bytes, int index) throws Exception {
if (bytes.length != 4)
throw new Exception("The length of the byte array must be at least 4 bytes long.");
return (int) ((int) (0xff & bytes[index]) << 56 | (int) (0xff & bytes[index + 1]) << 48 | (int) (0xff & bytes[index + 2]) << 40 | (int) (0xff & bytes[index + 3]) << 32);
}
public static byte[][] split(byte [] arrayIn, int len) {
if (arrayIn == null) {
return null;
}
boolean even = arrayIn.length % len == 0;
int totalLength = arrayIn.length / len;
if (!even)
totalLength++;
byte[][] newArray = new byte[totalLength][];
for (int i = 0; i < totalLength; ++i) {
int allocLength = len;
if (!even && i == totalLength - 1)
allocLength = arrayIn.length % len;
newArray[i] = new byte[allocLength];
System.arraycopy(arrayIn, i * len, newArray[i], 0, allocLength);
}
return newArray;
}
public static byte[][] split1dArray1(byte[] arr1D, int columnSize) {
byte[][] arr2D = null;
int rowSize = arr1D.length/columnSize;
for(int i = 0; i < rowSize; i++){
for(int j = 0; j < columnSize; j++){
arr2D[i][j] = arr1D[i+j];
}
}
return arr2D;
}
public static byte[][] split1dArray2(byte[] arr1D, int columnSize) {
int len=arr1D.length;
if(len%512 !=0) {
len= len/columnSize +1;
} else {
len=len/columnSize;
}
byte [][] arr2D= new byte [len][columnSize];
int k=0;
for(int i=0; i<=len-1; i++) {
for(int j=0; j<=columnSize-1; i++) {
arr2D[i][j]=arr1D[k];
k++;
}
}
return arr2D;
}
public static String toHexString(byte[] hexByte) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < hexByte.length; i++)
str.append(String.format("%x", hexByte[i]));
return str.toString();
}
private static byte [] test() {
// Command String
String hex = "84";
//Creates the command byte array (Function #1)
int NumberChars = hex.length();
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2) {
int x = i+2;
bytes[i / 2] = new BigInteger(hex.substring(i, x),16).byteValue();
}
return bytes;
}