|
|
|
@ -22,19 +22,35 @@ import java.util.regex.Pattern;
|
|
|
|
|
*/ |
|
|
|
|
public class ExchangeStringUtil { |
|
|
|
|
|
|
|
|
|
// public static void main(String args[]){
|
|
|
|
|
//// byte[] result = hexStrToBinaryStr("686868");
|
|
|
|
|
//// for (byte a :
|
|
|
|
|
//// result) {
|
|
|
|
|
//// System.out.println(a);
|
|
|
|
|
//// }
|
|
|
|
|
//// String str = "/192.168.1.252:30000";
|
|
|
|
|
//// String tempStr = str.replace("30000","40000");
|
|
|
|
|
//// String tempStr = str.substring(str.indexOf("/") + 1, str.lastIndexOf(":"));
|
|
|
|
|
// Double tempStr=parseHex4("FD00");
|
|
|
|
|
// System.out.println(tempStr);
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
public static void main(String args[]){ |
|
|
|
|
|
|
|
|
|
String s = floatToHex(12555.45F); |
|
|
|
|
System.out.println(s); |
|
|
|
|
System.out.println(hexToLong("0000006C")); |
|
|
|
|
System.out.println(longToHex(317751L)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 长整型转16进制 |
|
|
|
|
* @param value |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static String longToHex(long value) { |
|
|
|
|
String hexString = Long.toHexString(value); |
|
|
|
|
while (hexString.length() < 8) { |
|
|
|
|
hexString = "0" + hexString; |
|
|
|
|
} |
|
|
|
|
return hexString; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 16进制转长整型 |
|
|
|
|
* @param hexString |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static long hexToLong(String hexString) { |
|
|
|
|
return Long.parseLong(hexString, 16); // 将16进制字符串解析为长整型
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//十六进制转正负数十进制
|
|
|
|
|
public static double parseHex4(String num) { |
|
|
|
@ -1194,4 +1210,29 @@ public class ExchangeStringUtil {
|
|
|
|
|
return (flagValue); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 浮点转16进制 |
|
|
|
|
* @param value |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static String floatToHex(float value) { |
|
|
|
|
int intValue = Float.floatToIntBits(value) & 0xFFFFFFFF; |
|
|
|
|
String hexString = Integer.toHexString(intValue); |
|
|
|
|
while (hexString.length() < 8) { |
|
|
|
|
hexString = "0" + hexString; |
|
|
|
|
} |
|
|
|
|
return hexString; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 16进制转浮点 |
|
|
|
|
* @param hexString |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static float hexToFloat(String hexString) { |
|
|
|
|
int intBits = Integer.parseInt(hexString, 16); // 将16进制字符串解析为整数
|
|
|
|
|
return Float.intBitsToFloat(intBits); // 将整数值转换为浮点数
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|