java优先级对接_Java对接硬件接口,采用Scoket连接,数据报文通信
public classHexUtil {public static byte[] hexStringToBytes(String hexString) {if (hexString == null || hexString.equals("")) {return null;
}//toUpperCase將字符串中的所有字符轉換為大寫
hexString =hexString.toUpperCase();int length = hexString.length() / 2;//toCharArray將此字符串轉換為一個新的字符數組。
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]));
}returnd;
}//charToByte返回在指定字符的第一個發生的字符串中的索引,即返回匹配字符
private static byte charToByte(charc) {return (byte) "0123456789ABCDEF".indexOf(c);
}public static String BinaryToHexString(byte[] bytes) {
String hexStr= "0123456789ABCDEF";
String result= "";
String hex= "";for (byteb : bytes) {
hex= String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
hex+= String.valueOf(hexStr.charAt(b & 0x0F));
result+=hex ;
}returnresult;
}//兩位一字符,倒序排序
public staticString reverseString(String str) {
List strlist=newArrayList();char[] chr =str.toCharArray();for (int i = 0 ; i < chr.length; i=i+2) {
String s=chr[i]+""+chr[i+1];
strlist.add(s);
}
Collections.reverse(strlist);
String result="";for(String v:strlist){
result+=v;
}returnresult;
}/*** 16進制轉換成為string類型字符串
*@params
*@return
*/
public staticString hexStringToString(String s) {if (s == null || s.equals("")) {return null;
}
s= s.replace(" ", "");byte[] baKeyword = new byte[s.length() / 2];for (int i = 0; i < baKeyword.length; i++) {try{
baKeyword[i]= (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
}catch(Exception e) {
e.printStackTrace();
}
}try{
s= new String(baKeyword, "UTF-8");newString();
}catch(Exception e1) {
e1.printStackTrace();
}returns;
}/*** 字符串轉化成為16進制字符串
*@params
*@return
*/
public staticString strTo16(String s) {
String str= "";for (int i = 0; i < s.length(); i++) {int ch = (int) s.charAt(i);
String s4=Integer.toHexString(ch);
str= str +s4;
}returnstr;
}//將16進制字符串自動補全到8位 并且倒序排序
public staticString full8(String lenth) {int a =lenth.getBytes().length;int b = 8 -a;for (int i = 0; i < b; i++) {
lenth= "0" +lenth;
}returnreverseString(lenth);
}/*** xor運算
*
*@paramdata
*@return
*/
public static String getBCC(byte[] data) {
String ret= "";byte BCC[] = new byte[1];for (int i = 0; i < data.length; i++) {
BCC[0] = (byte) (BCC[0] ^data[i]);
}
String hex= Integer.toHexString(BCC[0] & 0xFF);if (hex.length() == 1) {
hex= '0' +hex;
}
ret+=hex.toUpperCase();returnret;
}
}
總結
以上是生活随笔為你收集整理的java优先级对接_Java对接硬件接口,采用Scoket连接,数据报文通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: decimal函数python_deci
- 下一篇: vue 表格内容跳转页面_Vue项目实战