package com.unistrong.demo.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by Gh0st on 2017/7/17.
* J1939协议处理相关的工具类
*/
public class J1939Utils {
public static String DebugHexByte2String(byte[] data) {
StringBuilder sb = new StringBuilder(data.length * 2);
int CurrentType = 2;
final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xff;
if (i == CurrentType) {
sb.append("type:");
} else if (i == CurrentType + 1) {
sb.append("currentLength:");
} else if (i == CurrentType + 3) {
sb.append("data:");
} else if (i == data.length - 4) {
sb.append("check:");
}
sb.append(HEX[value / 16]).append(HEX[value % 16]).append(" ");
}
return sb.toString();
}
public static String saveHex2String(byte[] data) {
StringBuilder sb = new StringBuilder(data.length * 2);
final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xff;
sb.append(HEX[value / 16]).append(HEX[value % 16]).append(" ");
}
return sb.toString();
}
/**
* @param s input string like : 000102030405060708
* @return byte[] b={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}
*/
public static byte[] int2bytes2(String s) {
byte[] data;
try {
s = s.replace(" ", "");
if (s.length() % 2 != 0) {
s = s.substring(0, s.length() - 1) + "0" + s.substring(s.length() - 1, s.length());
}
data = new byte[s.length() / 2];
for (int j = 0; j < data.length; j++) {
data[j] = (byte) (Integer.valueOf(s.substring(j * 2, j * 2 + 2), 16) & 0xff);
}
} catch (Exception e) {
e.printStackTrace();//NumberFormatException
data = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04};
}
return data;
}
/**
* @param data 0xf0
* @return f0
*/
public static String byte2String(byte data) {
StringBuilder sb = new StringBuilder();
final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
int value = data & 0xff;
sb.append(HEX[value / 16]).append(HEX[value % 16]).append(" ");
return sb.toString().trim();
}
/**
* @param byteString f0
* @return 0xf0
*/
public static byte string2byte(String byteString) {
byte b = 0;
if (byteString.length() == 2) {
b = (byte) (Integer.valueOf(byteString.substring(0, 2), 16) & 0xff);
}
return b;
}
public static int byte2int(byte b) {
return b & 0xff;
}
public static int moveRight(byte data, int length) {
return data >> length;
}
public static String getCurrentDateTimeString() {
DateFormat format = new SimpleDateFormat("[HH:mm:ss.SSS]", Locale.CHINA);
return format.format(new Date(System.currentTimeMillis()));
}
/**
* 245 -> 100
* 61440 -> F000
*
* @param n 进制
* @return 16进制字符串
*/
public static String int2HexString(int n) {
return RadixUtils.IntToHex(n);
}
/**
* input:100 -> 0x10 0x00 -> 245
* input:F000 -> 0xF0 0x00 -> 61440
*
* @param hexString F000
* @return 61440
*/
public static int hexString2Int(String hexString) {
return RadixUtils.HexToInt(hexString);
}
/**
* @param data 0x00 0x01 0x02 0x03
* @param start 0
* @param stop 2
* @return 0x00 0x01
*/
public static byte[] cutByteArray(byte[] data, int start, int stop) {
byte[] newData = null;
if (data != null && data.length > 0 && stop <= data.length && stop - start > 0) {
newData = new byte[stop - start];
System.arraycopy(data, start, newData, 0, stop - start);
}
return newData;
}
public static byte[] byteArrayAddByteArray(byte[] id, byte[] data) {
byte[] resultData = new byte[id.length + 1 + data.length];
System.arraycopy(id, 0, resultData, 0, id.length);
resultData[id.length] = (byte) data.length;
System.arraycopy(data, 0, resultData, id.length + 1, data.length);
return resultData;
}
/**
* 帧类型 标准数据帧 标准远程帧 扩展数据帧 扩展远程帧
* 判断远程帧 数据帧
*
* @param id
* @return
*/
public static int getFrameType(byte[] id) {
int intId = 0;
for (int i = 0; i < id.length; i++) {
intId = intId | id[i] << (8 * (id.length - 1 - i));
}
return intId & 0x0002;
}
/**
* id类型
* 判断标准帧 扩展帧
*
* @param id
* @return
*/
public static int getFrameFormat(byte[] id) {
int intId = 0;
for (int i = 0; i < id.length; i++) {
intId = intId | id[i] << (8 * (id.length - 1 - i));
}
return intId & 0x0004;
}
public static String getHex(String binary) {
StringBuilder sb = new StringBuilder();
int digitNumber = 1;
int sum = 0;
for (int i = 0; i < binary.length(); i++) {
if (digitNumber == 1)
sum += Integer.parseInt(binary.charAt(i) + "") * 8;
else if (digitNumber == 2)
sum += Integer.parseInt(binary.charAt(i) + "") * 4;
else if (digitNumber == 3)
sum += Integer.parseInt(binary.charAt(i) + "") * 2;
else if (digitNumber == 4 || i < binary.length() + 1) {
sum += Integer.parseInt(binary.charAt(i) + "") * 1;
digitNumber = 0;
if (sum < 10)
sb.append(sum);
else if (sum == 10)
sb.append("A");
else if (sum == 11)
sb.append("B");
else if (sum == 12)
sb.append("C");
else if (sum == 13)
sb.append("D");
else if (sum == 14)
sb.append("E");
else if (sum == 15)
sb.append("F");
sum = 0;
}
digitNumber++;
}
return sb.toString();
}
}
package com.unistrong.uarttest.utils;
/**
* Created by gh0st on 2017/12/6.
* http://blog.csdn.net/mfcing/article/details/48177351
*/
public class RadixUtils {
//10进制转16进制
protected static String IntToHex(int n) {
char[] ch = new char[20];
int nIndex = 0;
while (true) {
int m = n / 16;
int k = n % 16;
if (k == 15)
ch[nIndex] = 'F';
else if (k == 14)
ch[nIndex] = 'E';
else if (k == 13)
ch[nIndex] = 'D';
else if (k == 12)
ch[nIndex] = 'C';
else if (k == 11)
ch[nIndex] = 'B';
else if (k == 10)
ch[nIndex] = 'A';
else
ch[nIndex] = (char) ('0' + k);
nIndex++;
if (m == 0)
break;
n = m;
}
StringBuffer sb = new StringBuffer();
sb.append(ch, 0, nIndex);
sb.reverse();
return sb.toString();
}
//16进制转10进制
protected static int HexToInt(String strHex) {
int nResult = 0;
if (!IsHex(strHex))
return nResult;
String str = strHex.toUpperCase();
if (str.length() > 2) {
if (str.charAt(0) == '0' && str.charAt(1) == 'X') {
str = str.substring(2);
}
}
int nLen = str.length();
for (int i = 0; i < nLen; ++i) {
char ch = str.charAt(nLen - i - 1);
try {
nResult += (GetHex(ch) * GetPower(16, i));
} catch (Exception e) {
e.printStackTrace();
}
}
return nResult;
}
//计算16进制对应的数值
protected static int GetHex(char ch) throws Exception {
if (ch >= '0' && ch <= '9')
return (int) (ch - '0');
if (ch >= 'a' && ch <= 'f')
return (int) (ch - 'a' + 10);
if (ch >= 'A' && ch <= 'F')
return (int) (ch - 'A' + 10);
throw new Exception("error param");
}
//计算幂
protected static int GetPower(int nValue, int nCount) throws Exception {
if (nCount < 0)
throw new Exception("nCount can't small than 1!");
if (nCount == 0)
return 1;
int nSum = 1;
for (int i = 0; i < nCount; ++i) {
nSum = nSum * nValue;
}
return nSum;
}
//判断是否是16进制数
protected static boolean IsHex(String strHex) {
int i = 0;
if (strHex.length() > 2) {
if (strHex.charAt(0) == '0' && (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x')) {
i = 2;
}
}
for (; i < strHex.length(); ++i) {
char ch = strHex.charAt(i);
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
continue;
return false;
}
return true;
}
}
网友评论