/**
* Ascii工具类
*
* @author jilke
*/
public class AsciiUtils {
/**
* 无符号整数ASC转String
* @param hex
* @return
*/
public static String hexTo10(String hex) {
String myStr[] = {"a", "b", "c", "d", "e", "f"};
int result = 0;
int n = 1;
for (int i = hex.length() - 1; i >= 0; i--) {
String param = hex.substring(i, i + 1);
for (int j = 0; j < myStr.length; j++) {
if (param.equalsIgnoreCase(myStr[j])) {
param = "1" + String.valueOf(j);
}
}
result += Integer.parseInt(param) * n;
n *= 16;
}
return String.valueOf(result);
}
/**
* 有符号asc码转String(业务需要除以10保留一位小数点)
* @param hexString
* @return
*/
public static String hexToDecimal(String hexString) {
int decimal = Integer.parseInt(hexString, 16);
if ((decimal & 0x8000) != 0) { // 判断最高位是否为1
decimal = -(0x10000 - decimal); // 负数处理
}
// 除以10保留一位小数点
double result = (double) decimal / 10.0;
return String.valueOf(result);
}
public static void main(String[] args) {
String ffc9 = hexToDecimal("FFC9");
System.out.println(ffc9);
}
/**
* int转asc码string
* @param decimal
* @return
*/
public static String decimalToHex(int decimal) {
String hexString = Integer.toHexString(decimal);
if (decimal < 0) {
hexString = hexString.substring(4); // 截取后四位,因为负数的情况下 toHexString 方法返回的字符串长度为8
while (hexString.length() < 4) {
hexString = "0" + hexString; // 在负数的情况下补足为4位
}
}
return hexString.toUpperCase();
}
/**
* int转asc码string
* @param decimal
* @param n 长度补零
* @return
*/
public static String decimalToHex(int decimal, int n) {
String hexString = Integer.toHexString(decimal);
if (decimal < 0) {
hexString = hexString.substring(4); // 截取后四位,因为负数的情况下 toHexString 方法返回的字符串长度为8
while (hexString.length() < n) {
hexString = "0" + hexString; // 在负数的情况下补足为 N 位
}
} else {
while (hexString.length() < n) {
hexString = "0" + hexString; // 在正数的情况下补足为 N 位
}
}
return hexString.toUpperCase();
}
/**
* VHF语句校验
* @param sentence 语句内容
* 1-异或校验异常 MessageVerificationException
* 2-语句异常 MessageFormatException
* 3-消息号异常 MessageNoException
* 4-消息长度异常 MessageLengthException
* 5-其他异常 Exception
* @return
* @throws MessageVerificationException
* @throws MessageFormatException
*/
public static boolean checkVhf(String sentence) throws MessageVerificationException, MessageFormatException {
String[] strs = sentence.split(",");
if (strs.length > 0 && strs[0].length() == 6 && strs[1].contains("*")) {
String verification = sentence.substring(sentence.indexOf("*") + 1, sentence.indexOf("*") + 3);
String checkStr = sentence.substring(1, sentence.indexOf("*"));
String verificationCode = verification(checkStr);
if (!verification.equals(verificationCode)) {
return false;
}
} else {
return false;
}
return true;
}
public static String verification(String str) {
int c1 = str.charAt(0);
for(int i = 1; i < str.length(); ++i) {
int c = str.charAt(i);
c1 ^= c;
}
String verificationCode = Long.toHexString((long)c1).toUpperCase();
if (verificationCode.length() < 2) {
verificationCode = "0" + verificationCode;
}
return verificationCode;
}
/*
*
* VHF报文校验
*
*/
public static String verifications(String str) {
int c1 = str.charAt(0);
for (int i = 1; i < str.length(); i++) {
int c = str.charAt(i);
c1 = c1 ^ c;
}
String verificationCode = Long.toHexString(c1).toUpperCase();
if (verificationCode.length() < 2) {
verificationCode = "0" + verificationCode;
}
return verificationCode;
}
}
ASCii转码工具类
最新推荐文章于 2024-09-13 14:39:26 发布