package ng.devices;
|
|
import java.text.DecimalFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
public class HexUtil {
|
|
public static String formatHex(String hex) {
|
String result = "";
|
|
for (int i = 0; i < hex.length() - 1; i+=2) {
|
String output = hex.substring(i, i + 2);
|
result += ("0x" + output + " ");
|
}
|
|
if (result.length() > 0) {
|
result = result.substring(0, result.lastIndexOf(" "));
|
}
|
|
return result;
|
}
|
|
//æ£å¸¸ç忝æ°ååæåèæ°åé?ç»çå
|
public static String asciiToHex(String str) {
|
char[] chars = str.toCharArray();
|
|
StringBuffer hex = new StringBuffer();
|
for (int i = 0; i < chars.length; i++) {
|
hex.append(Integer.toHexString(chars[i]));
|
}
|
|
return hex.toString();
|
}
|
|
public static String hexToAscii(String hex) {
|
StringBuffer result = new StringBuffer();
|
|
for (int i = 0; i < hex.length() - 1; i+= 2) {
|
String output = hex.substring(i, i + 2);
|
int decimal = Integer.parseInt(output, 16);
|
result.append((char) decimal);
|
}
|
|
return result.toString();
|
}//12300 //00321
|
//äºè¿å¶è½¬åè¿å?
|
public static int int2ToHex(String number) {
|
return Integer.parseInt(number, 2);
|
}
|
//äºè¿å¶è½¬16è¿å¶ 4ä½?
|
public static String intBinaryTo16(String number) {
|
int num=int2ToHex(number);
|
return intTo2ByteHex(num);
|
}
|
//åå
è¿å¶è½¬åè¿å¶
|
public static int int16ToHex(String number) {
|
return Integer.parseInt(number, 16);
|
}
|
//åè¿å¶è½¬äºè¿å?
|
public static String intToBinary(int number) {
|
return Integer.toBinaryString(number);
|
}
|
//åå
è¿å¶è½¬äºè¿å¶
|
public static String int16ToBinary(String number) {
|
return intToBinary(int16ToHex(number));
|
}
|
//åå
è¿å¶è½¬äºè¿å¶
|
public static String int16ToBinaryEight(String number,int count) {
|
String binary=int16ToBinary(number);
|
String zero="";
|
for (int i = 0; i <count-binary.length(); i++) {
|
zero+="0";
|
}
|
binary=zero+binary;
|
return binary;
|
}
|
|
public static String intToHex(int number) {
|
return Integer.toHexString(number);
|
}
|
|
/**
|
* å°æ´æ°è½¬æ¢ä¸º2ä½?16è¿å¶ï¼å¦1转æ¢ä¸?01ï¼?10转æ¢ä¸?0a
|
*
|
* @param number
|
* @return
|
*/
|
public static String intTo1ByteHex(int number) {
|
String numberHex = HexUtil.intToHex(number);
|
|
numberHex = String.format("%2s", numberHex).replace(' ', '0');
|
|
return numberHex;
|
}
|
|
/**
|
* å°æ´æ°è½¬æ¢ä¸º4ä½?16è¿å¶ï¼å¦1转æ¢ä¸?0001ï¼?10转æ¢ä¸?000a
|
*
|
* @param number
|
* @return
|
*/
|
public static String intTo2ByteHex(int number) {
|
String numberHex = HexUtil.intToHex(number);
|
|
numberHex = String.format("%4s", numberHex).replace(' ', '0');
|
|
return numberHex;
|
}
|
|
/**
|
* å°æ´æ°è½¬æ¢ä¸º8ä½?16è¿å¶ï¼å¦1转æ¢ä¸?00000001ï¼?10转æ¢ä¸?0000000a
|
*
|
* @param number
|
* @return
|
*/
|
public static String intTo4ByteHex(int number) {
|
String numberHex = HexUtil.intToHex(number);
|
numberHex = String.format("%8s", numberHex).replace(' ', '0');
|
return numberHex;
|
}
|
|
/**
|
* å°æ¶é´è½¬æ¢ä¸º16è¿å¶æ ¼å¼ï¼å¹´ï¼?2åèï¼? + æï¼1åèï¼? + æ¥ï¼1åèï¼? + æ¶ï¼1åèï¼? + åï¼1åèï¼? + ç§ï¼1åèï¼?
|
*
|
* @param time
|
* @return
|
*/
|
public static String timeToHex(Date time) {
|
Calendar c = Calendar.getInstance();
|
c.setTime(time);
|
|
String yearHex = intTo2ByteHex(c.get(Calendar.YEAR));
|
String monthHex = intTo1ByteHex(c.get(Calendar.MONTH) + 1);
|
String dayHex = intTo1ByteHex(c.get(Calendar.DAY_OF_MONTH));
|
String hourHex = intTo1ByteHex(c.get(Calendar.HOUR_OF_DAY));
|
String minuteHex = intTo1ByteHex(c.get(Calendar.MINUTE));
|
String secondHex = intTo1ByteHex(c.get(Calendar.SECOND));
|
|
return yearHex + monthHex + dayHex + hourHex + minuteHex + secondHex;
|
}
|
|
/**
|
* å°?16è¿å¶çæ¶é´è½¬æ¢ä¸ºyyyy-MM-dd HH:mm:ssçæ ¼å¼?
|
*
|
* @param hexTime
|
* @return
|
*/
|
public static String hexToTime(String hexTime) {
|
String year = hexTo4DigitInt(hexTime.substring(0, 4));
|
String month = hexTo2DigitInt(hexTime.substring(4, 6));
|
String day = hexTo2DigitInt(hexTime.substring(6, 8));
|
String hour = hexTo2DigitInt(hexTime.substring(8, 10));
|
String minute = hexTo2DigitInt(hexTime.substring(10, 12));
|
String second = hexTo2DigitInt(hexTime.substring(12, 14));
|
|
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
|
}
|
//åå
è¿å¶è½?10è¿å¶
|
public static int hexToInt(String hex) {
|
return Integer.parseInt(hex, 16);
|
}
|
//åå
è¿å¶è½¬æ10è¿å¶åä½
|
public static String hexTo4DigitInt(String hex) {
|
return new DecimalFormat("0000").format(hexToInt(hex));
|
}
|
//åå
è¿å¶è½¬æ10è¿å¶ä¸¤ä½
|
public static String hexTo2DigitInt(String hex) {
|
return new DecimalFormat("00").format(hexToInt(hex));
|
}
|
|
public static byte[] stringToInt(String a){
|
byte[] byt = new byte[a.length()/2];
|
for (int i = 0; i < a.length() - 1; i+=2) {
|
String output = a.substring(i, i + 2);
|
byt[i/2]=(byte)Integer.parseInt(output, 16);
|
}
|
|
return byt;
|
}
|
|
/**
|
* å°åè转æ¢ä¸ºä¸¤ä½åå
è¿å¶å符串ï¼ä¸å¤ä½åå?0
|
*
|
* @param b
|
* @return
|
*/
|
public static String byteToHexString(byte b) {
|
String hex = Integer.toHexString(b & 0xFF);
|
|
if (hex.length() == 1) {
|
hex = "0" + hex;
|
}
|
|
return hex;
|
}
|
|
/**
|
* å°åå
è¿å¶å符ï¼è½¬æ¢æäºè¿å¶çåç¬?
|
*
|
* @param b
|
* @return
|
*/
|
public static String getBin(String hex){
|
String[] hexs = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
"a", "b", "c", "d", "e", "f"};
|
String[] bins = new String[]{"0000", "0001", "0010", "0011", "0100", "0101",
|
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
|
int i;
|
for(i=0; i<hexs.length && !hex.toLowerCase().equals(hexs[i]); i++);
|
return bins[i];
|
}
|
|
/**
|
* å°åå
è¿å¶å符ï¼è½¬æ¢æäºè¿å¶çåç¬?
|
*
|
* @param b
|
* @return
|
*/
|
public static String gethex(String hex){
|
String[] hexs = new String[]{"0000", "0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009",
|
"000a", "000b", "000c", "000d", "000e", "000f"};
|
String[] bins = new String[]{"0000", "0001", "0010", "0011", "0100", "0101",
|
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
|
int i;
|
for(i=0; i<bins.length && !hex.toLowerCase().equals(bins[i]); i++);
|
return hexs[i];
|
}
|
|
|
|
/**
|
* åè¿å¶åèæ°ç»è½¬åå
è¿å¶å符ä¸?
|
* @param bufSize
|
* @param msg
|
* @return
|
*/
|
public static String byteToHexString(int bufSize,byte[] msg){
|
String tempHex = "";
|
String command = "";
|
if (bufSize != -1) {
|
for (int i = 0; i < bufSize; i++) {
|
tempHex = Integer.toHexString(msg[i] & 0xFF);
|
|
if (tempHex.length() == 1) {
|
tempHex = "0" + tempHex;
|
}
|
command += tempHex;
|
}
|
}
|
return command;
|
}
|
|
public static String hexToBinary(String hexString) {
|
String binaryString = Integer.toBinaryString(hexToInt(hexString));
|
return String.format("%16s", binaryString).replace(' ', '0');
|
}
|
|
public static String binaryTo2ByteHex(String binaryString) {
|
String HexString = Integer.toHexString(Integer.parseInt(binaryString, 2));
|
return String.format("%4s", HexString).replace(' ', '0');
|
}
|
|
public static void main(String[] args) {
|
// System.out.println(HexUtil.hexToAscii("3c5354413e48656c6c6f20576f726c64217c5468697320697320746865206669727374207369676e616c2066726f6d20646576696365213c454f463e"));
|
// System.out.println(HexUtil.asciiToHex("<STA>Hello World!|This is the first signal from device!<EOF>"));
|
//
|
// System.out.println(String.format("%4S", HexUtil.intToHex(55)).replace(' ', '0'));
|
// System.out.println(HexUtil.hexToInt("00d2"));
|
//
|
// System.out.println(HexUtil.formatHex("3c5354413e"));
|
String message = "Hello World!|This is the first signal from device!";
|
int length = message.length() * 2 + 10; // é¿åº¦å
æ¬ç»å°¾ç?<EOF>ï¼ä¸ä¸ªå符å¨ä¿¡å·ä¸ç±ä¸¤ä¸ªåè表示ã?
|
|
String command = HexUtil.asciiToHex("<STA>"); // æ·»å å¼?å§æ è¯?
|
|
command += (String.format("%4s", HexUtil.intToHex(length)).replace(' ', '0')); // æ·»å é¿åº¦æ è¯
|
command += "01"; // æ·»å 设å¤ç±»å 0x01表示å岿ºï¼0x02表示é¢åç?
|
command += "0001"; // æ·»å 设å¤åå·ï¼?0x0001è¡¨ç¤ºåºæ¬æ¬¾ï¼0x0002表示ä¸çº§æ¬?
|
command += "0000"; // åè½å·ï¼0x0000æ¯ä¸ä½æºä¸»å¨åç»ä¸ä½æºï¼0x0001æ¯ä¸ä½æºä¿®æ¹ä¸ä½æºRTC
|
command += "00"; // å 坿¹å¼ï¼?0x00表示ä¸å å¯?
|
|
Calendar c = Calendar.getInstance();
|
|
// æ·»å æ¶é´ï¼å¹¶å°æ¶é´è½¬æ¢ä¸º16è¿å¶è¡¨ç¤º
|
command += (String.format("%4s", HexUtil.intToHex(c.get(Calendar.YEAR))).replace(' ', '0'));
|
command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.MONTH) + 1)).replace(' ', '0'));
|
command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.DAY_OF_MONTH))).replace(' ', '0'));
|
command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.HOUR_OF_DAY))).replace(' ', '0'));
|
command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.MINUTE))).replace(' ', '0'));
|
command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.SECOND))).replace(' ', '0'));
|
|
command += ("00000001"); // å¯ä¸æ è¯ï¼ä½¿ç¨åºåå·
|
|
command += (HexUtil.asciiToHex(message)); // æ·»å 主è¦ä¿¡æ¯å
容
|
|
command += (HexUtil.asciiToHex("<EOF>")); // æ·»å ç»å°¾æ è¯
|
|
System.out.println(command);
|
|
System.out.println(String.format("%4s", "1"));
|
|
Date time = new Date();
|
System.out.println(new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(time));
|
|
String timeHex = timeToHex(time);
|
System.out.println(timeHex);
|
System.out.println(hexToTime(timeHex));
|
|
System.out.println("3c5354413e001a00000000000000000000000000010007e0021500152800000000000000005041001d017c017c01017c3c454f463e".length());
|
|
System.out.println(hexToBinary("0c0a"));
|
|
System.out.println(Integer.MAX_VALUE);
|
}
|
|
public static String hexTo2Binary(String hexString) {
|
String binaryString = Integer.toBinaryString(hexToInt(hexString));
|
return String.format("%8s", binaryString).replace(' ', '0');
|
}
|
}
|