在开发过程中有时会经常需要做数据转换,最经常使用的就是对接一些硬件设备。硬件设备通讯一般都是使用字节数组,十六进制数据,二进制数据等。
/*
* Create 7/18 by xwj
* 数据处理,进制转换等常用工具类
*/
public class DataTreater {
public static String byte2String(byte[] bytes, int icnt) {
//转化byte[]为常用参数十进制字符串
Integer tempData = Integer.valueOf(formatBytes(bytes), 16);
String tempStr = tempData.toString();
for (int i = 0; i < icnt - tempData.toString().length(); ++i) {
tempStr = "0" + tempStr;
}
return tempStr;
}
public static String hexToinvalue(byte[] src, int offset) {
//十六进制数组转化为十进制小数
int invalue = bytesToInt(src, offset);
return IntStrToAmountStr(invalue + "");
}
public static int bytesToInt(byte[] src, int offset) {
int value;
value = (int) ((src[offset] & 0xFF)
| ((src[offset + 1] & 0xFF) << 8));
return value;
}
public static int bytesToInt(byte src) {
int value;
value = (int) (src & 0xFF);
return value;
}
public static String BCD2String(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; ++i) {
temp.append((byte) ((bytes[i] & 240) >>> 4));
temp.append((byte) (bytes[i] & 15));
}
return temp.toString();
}
public static byte decimalToBinary(int decimalSource) {
BigInteger bi = new BigInteger(String.valueOf(decimalSource));
return bit2byte(bi.toString(2));
}
public static byte bit2byte(String bString) {
byte result = 0;
int i = bString.length() - 1;
for (int j = 0; i >= 0; ++j) {
result = (byte) ((int) ((double) result + (double) Byte.parseByte(String.valueOf(bString.charAt(i))) * Math.pow(2.0D, (double) j)));
--i;
}
return result;
}
public static String HexStrToAmountStr(String s) {
long Amount = Long.parseLong(s, 16);
String d = String.valueOf(Amount / 100L);
if (Amount % 100L < 10L) {
d = d + ".0" + Amount % 100L;
} else {
d = d + "." + Amount % 100L;
}
return d;
}
public static String IntStrToAmountStr(String intstr) {
int amount = Integer.valueOf(intstr);
String d = String.valueOf(amount / 10);
d = d + "." + amount % 10;
return d;
}
public static String toStringHex(String s) {
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; ++i) {
try {
baKeyword[i] = (byte) (255 & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception var5) {
var5.printStackTrace();
}
}
try {
s = new String(baKeyword, "utf-8");
} catch (Exception var4) {
var4.printStackTrace();
}
return s;
}
public static short[] toShortArray(byte[] src) {
//byte数组转化为short数组
int count = src.length >> 1;
short[] dest = new short[count];
for (int i = 0; i < count; i++) {
dest[i] = (short) (((src[i * 2 + 1] & 0x00FF) << 8) | (src[2 * i] & 0x00FF));
}
return dest;
}
public static short toShort(String hexstr) {
return (short) Integer.parseInt(hexstr, 16);
}
public static int byteArrayToShort(byte[] b) {
return (b[0] << 8)
+ (b[1] & 0xFF);
}
public static short byteToShort(byte high, byte low) {
return (short) (((high << 8) & 0xFF00) | (low & 0xFF));
}
public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; ++i) {
String hex = Integer.toHexString(255 & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
public static byte[] hexStringToBytes(String hexString) {
if (hexString != null && !hexString.equals("")) {
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;
} else {
return null;
}
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static String formatBytes(byte[] p_arrBytes) {
//转化byte[]为十六进制字符串
return formatBytes(p_arrBytes, p_arrBytes.length, "");
}
public static String byteToHexStr(byte b) {
//字节转换为16进制字符
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex.toUpperCase();
}
public static void printHexString(byte[] b) {
//字节数组转换为16进制字符串
for (int i = 0; i < b.length; i++) {
byte hex = b[i];
}
}
public static String toHexString(byte b) {
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1) {
return "0" + s;
} else {
return s;
}
}
public static String formatBytes(byte p_Bytes) {
StringBuffer sbResult = new StringBuffer();
int intValue = p_Bytes;
if (p_Bytes < 0) {
intValue = p_Bytes + 256;
}
String strHexString = Integer.toHexString(intValue);
if (strHexString.length() == 1) {
sbResult.append("0");
}
sbResult.append(strHexString);
return sbResult.toString();
}
public static String formatBytes(byte[] p_arrBytes, int p_intLength, String p_strSeparator) {
StringBuffer sbResult = new StringBuffer();
for (int intIndex = 0; intIndex < p_intLength; ++intIndex) {
int intValue = p_arrBytes[intIndex];
if (intValue < 0) {
intValue += 256;
}
String strHexString = Integer.toHexString(intValue);
if (strHexString.length() == 1) {
sbResult.append("0");
sbResult.append(p_strSeparator);
}
sbResult.append(strHexString);
}
return sbResult.toString();
}
public static byte[] compress2To1InBytes(String p_strValue) throws Exception {
int intLength = p_strValue.length();
int intCounter = 0;
int intResultLength = intLength / 2 + (intLength % 2 != 0 ? 1 : 0);
byte[] arrResult = new byte[intResultLength];
for (int intIndex = intLength; intIndex >= 1; intIndex -= 2) {
int intValue;
if (intIndex == 1) {
intValue = Integer.parseInt(p_strValue.substring(intIndex - 1, intIndex));
} else {
intValue = Integer.parseInt(p_strValue.substring(intIndex - 2, intIndex));
}
arrResult[intResultLength - intCounter - 1] = (byte) (intValue + 33);
++intCounter;
}
return arrResult;
}
public static String compress2To1(String p_strValue) throws Exception {
return compress2To1(p_strValue, "iso8859_1");
}
public static String compress2To1(String p_strValue, String p_strEncoding) throws Exception {
return new String(compress2To1InBytes(p_strValue), p_strEncoding);
}
}