package tools;
|
|
import java.math.BigInteger;
|
|
@SuppressWarnings("unused")
|
public class Tools {
|
|
|
|
/**
|
* ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×é
|
*
|
* @param hexString
|
* @return
|
*/
|
public static byte[] hexStrToBinaryStr(String hexString) {
|
|
|
if (hexString==null) {
|
return null;
|
}
|
|
hexString = hexString.replaceAll(" ", "");
|
|
int len = hexString.length();
|
int index = 0;
|
|
|
byte[] bytes = new byte[len / 2];
|
|
while (index < len) {
|
|
String sub = hexString.substring(index, index + 2);
|
bytes[index/2] = (byte)Integer.parseInt(sub,16);
|
index += 2;
|
}
|
|
|
return bytes;
|
}
|
|
/**
|
* ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×鲻ȥ³ý¿Õ¸ñ
|
*
|
* @param hexString
|
* @return
|
*/
|
public static byte[] hexStrToBinaryStr2(String hexString) {
|
|
if (hexString==null) {
|
return null;
|
}
|
|
int len = hexString.length();
|
|
int index = 0;
|
|
|
byte[] bytes = new byte[len / 2];
|
|
while (index < len) {
|
String sub = hexString.substring(index, index + 2);
|
bytes[index/2] = (byte)Integer.parseInt(sub,16);
|
index += 2;
|
}
|
|
|
return bytes;
|
}
|
|
|
|
public static final int registersToInt(byte[] bytes) {
|
return (
|
((bytes[0] & 0xff) << 24) |
|
((bytes[1] & 0xff) << 16) |
|
((bytes[2] & 0xff) << 8) |
|
(bytes[3] & 0xff)
|
);
|
}//registersToInt
|
|
|
// hexתASCII
|
public static String hexToAscii(String hex) {
|
StringBuilder ascii = new StringBuilder();
|
// È¥³ý HEX ×Ö·û´®ÖеĿոñ
|
hex = hex.replaceAll(" ", "");
|
|
// ÿÁ½¸ö×Ö·ûΪһ×飬ת»»Îª¶ÔÓ¦µÄ×Ö½Ú²¢Æ´½Ó³É ASCII ×Ö·û
|
for (int i = 0; i < hex.length(); i += 2) {
|
// Ö±½ÓʹÓà charAt »ñȡÿÁ½¸ö×Ö·ûµÄÊ®Áù½øÖÆÊý×Ö
|
char hexChar1 = hex.charAt(i);
|
char hexChar2 = hex.charAt(i + 1);
|
|
// ×éºÏÊ®Áù½øÖÆ×Ö·ûΪһ¸öÊý×Ö
|
int decimalValue = (Character.digit(hexChar1, 16) << 4) + Character.digit(hexChar2, 16);
|
|
// ת»»Îª¶ÔÓ¦µÄ ASCII ×Ö·û²¢Ìí¼Óµ½½á¹ûÖÐ
|
ascii.append((char) decimalValue);
|
}
|
|
return ascii.toString(); // ·µ»ØÆ´½ÓºóµÄ ASCII ×Ö·û´®
|
}
|
|
|
|
public static String byteToAscii(byte[] bytes) {
|
// ½«×Ö½ÚÊý×éת»»Îª ASCII ×Ö·û´®
|
String asciiString = new String(bytes, java.nio.charset.StandardCharsets.US_ASCII);
|
return asciiString;
|
}
|
|
/**½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®*/
|
public static String Bytes2HexString(byte[] b) {
|
String ret = "";
|
for (int i = 0; i < b.length; i++) {
|
String hex = Integer.toHexString(b[i] & 0xFF);
|
if (hex.length() == 1) {
|
hex = '0' + hex;
|
}
|
ret += hex.toUpperCase();
|
}
|
return ret;
|
}
|
|
|
|
|
|
|
|
public static final float registersToFloat(byte[] bytes) {
|
return Float.intBitsToFloat((
|
((bytes[0] & 0xff) << 24) |
|
((bytes[1] & 0xff) << 16) |
|
((bytes[2] & 0xff) << 8) |
|
(bytes[3] & 0xff)
|
));
|
}//
|
@SuppressWarnings("unused")
|
private static String intToHex(int n) {
|
StringBuffer s = new StringBuffer();
|
String a;
|
char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
|
while(n != 0){
|
s = s.append(b[n%16]);
|
n = n/16;
|
}
|
a = s.reverse().toString();
|
return a;
|
}
|
|
//½«·µ»ØµÄÊý¾Ýת»¯Îª10½øÖÆÊý¾Ý
|
public static int ormntoint(byte[] bytes){
|
int aa=0;
|
aa=bytes[0]-48 +(bytes[1]-48)*10+(bytes[2]-48)*100+(bytes[3]-48)*1000;
|
return aa;
|
}
|
|
|
/**
|
* 16½øÖƵÄ×Ö·û´®±íʾת³É×Ö½ÚÊý×é
|
*
|
* @param hexString 16½øÖƸñʽµÄ×Ö·û´®
|
* @return ת»»ºóµÄ×Ö½ÚÊý×é
|
**/
|
public static byte[] toByteArray(String hexString) {
|
byte[] byteArray=null;
|
if(hexString !=null || hexString !="") {
|
hexString = hexString.replaceAll(" ", "");
|
byteArray = new byte[hexString.length() / 2];
|
int k = 0;
|
for (int i = 0; i < byteArray.length; i++) {//ÒòΪÊÇ16½øÖÆ£¬×î¶àÖ»»áÕ¼ÓÃ4λ£¬×ª»»³É×Ö½ÚÐèÒªÁ½¸ö16½øÖƵÄ×Ö·û£¬¸ßλÔÚÏÈ
|
byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
|
byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
|
byteArray[i] = (byte) (high << 4 | low);
|
k += 2;
|
}
|
}
|
return byteArray;
|
}
|
|
|
public static byte[] intToRegisters(int v) {
|
byte[] registers = new byte[4];
|
registers[0] = (byte) (0xff & (v >> 24));
|
registers[1] = (byte) (0xff & (v >> 16));
|
registers[2] = (byte) (0xff & (v >> 8));
|
registers[3] = (byte) (0xff & v);
|
return registers;
|
}
|
|
|
|
//ת³É×Ö½ÚÊý×é
|
public static String[] hex(String message) {
|
int size=message.length()/2;
|
String[] hex=new String[size];
|
for(int i=0;i<size;i++) {
|
hex[i]=message.substring(i*2, 2+i*2);
|
}
|
|
return hex;
|
}
|
|
|
/**
|
* 16½øÖÆ×ªÎª10½øÖÆ
|
*/
|
public static int decodeHEX(String hexs) {
|
BigInteger bigint = new BigInteger(hexs, 16);
|
int numb = bigint.intValue();
|
return numb;
|
}
|
|
public static byte decodeHEXToInt8(String hexStr) {
|
BigInteger bigInt = new BigInteger(hexStr, 16);
|
// ½«ÎÞ·ûºÅÕûÊýת»»ÎªÓзûºÅµÄ8λÕûÊý
|
byte result = bigInt.byteValue();
|
return result;
|
}
|
}
|