package com.firstbuild.tools;
/**
* Created by hans on 16. 7. 7..
*/
public class MathTools {
// hex to byte[]
public static byte[] hexToByteArray(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
byte[] ba = new byte[hex.length() / 2];
for (int i = 0; i < ba.length; i++) {
ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
}
return ba;
}
/**
* Convert byte Array to HEX decimal
* Converted value's Byte Order starts from the left. Left value is lower byte to the Right value
* @param ba byte array to convert
* @return Converted Hexa Decimal value
*/
public static String byteArrayToHex(byte[] ba) {
if (ba == null || ba.length == 0) {
return null;
}
StringBuffer sb = new StringBuffer(ba.length * 2);
String hexNumber;
for (int x = 0; x < ba.length; x++) {
hexNumber = "0" + Integer.toHexString(0xff & ba[x]);
sb.append(hexNumber.substring(hexNumber.length() - 2));
}
return sb.toString();
}
public static int hexByteToInt(byte b) {
return Integer.parseInt(Byte.toString(b), 16);
}
}