ModBus-rtcp/src/test/java/com/iot/modbus_rtcp/GatewayTest.java

41 lines
1.2 KiB
Java

package com.iot.modbus_rtcp;
import com.iot.modbus_rtcp.utils.HexUtil;
import org.junit.jupiter.api.Test;
/**
* @author <a href="mailto:shilong.wang@alpha-ess.com">王仕龙</a>
* 2024/11/28 18:52
*/
public class GatewayTest {
@Test
public void testConverterHeartbeat() {
String s1 = "4B454E454E4731343030303030333538";
String s2 = "KENENG1400000358";
System.out.println(new String(HexUtil.hexStringToBytes(s1)));
System.out.println(HexUtil.bytesToHexString(s2.getBytes()));
String hex = "333538";
int decimal = 0;
int power = 0;
for (int i = hex.length() - 1; i >= 0; i--) {
char digit = hex.charAt(i);
if (digit >= '0' && digit <= '9') {
decimal += (digit - '0') * Math.pow(16, power);
} else if (digit >= 'A' && digit <= 'F') {
decimal += (digit - 'A' + 10) * Math.pow(16, power);
} else if (digit >= 'a' && digit <= 'f') {
decimal += (digit - 'a' + 10) * Math.pow(16, power);
} else {
throw new IllegalArgumentException("Invalid hex digit: " + digit);
}
power++;
}
System.out.println(decimal);
}
}