only:hex progress

This commit is contained in:
rusefillc 2023-07-09 19:13:59 -04:00
parent 88018542a6
commit 9021ae9c7a
4 changed files with 26 additions and 6 deletions

View File

@ -1,6 +1,10 @@
package com.rusefi.io.can;
public class HexUtil {
private static final String HEX_STRING = "0123456789ABCDEF";
public static final byte[] HEX_BYTE_ARRAY = HEX_STRING.getBytes();
public static final char[] HEX_CHAR_ARRAY = HexUtil.HEX_STRING.toCharArray();
public static byte[] asBytes(String hex) {
if (hex.indexOf(' ') >= 0) {
hex = hex.replaceAll(" ", "");
@ -11,6 +15,16 @@ public class HexUtil {
return hexToBytes(hex);
}
public static String asString(byte[] input) {
StringBuilder sb = new StringBuilder();
for (byte b : input) {
int i = b & 0xff;
sb.append(HEX_STRING.charAt(i / 16));
sb.append(HEX_STRING.charAt(i % 16));
}
return sb.toString();
}
public static byte[] hexToBytes(CharSequence s) {
return hexToBytes(s, 0);
}

View File

@ -25,9 +25,7 @@ public class Elm327Connector implements Closeable {
log.configureDebugEnabled(false);
}
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
// public final static int ELM327_DEFAULT_BAUDRATE = 115200; // OBDlink SX, 1.3a STN1110
// public final static int ELM327_DEFAULT_BAUDRATE = 115200; // OBDlink SX, 1.3a STN1110
public final static int ELM327_DEFAULT_BAUDRATE = 38400;
private final static int BIG_TIMEOUT = 2 * SECOND;
private final static int TIMEOUT = 70;
@ -243,8 +241,8 @@ public class Elm327Connector implements Closeable {
for (int i = 0; i < length; i++) {
int j = i * 2;
int v = data[i] & 0xFF;
hexData[j] = HEX_ARRAY[v >>> 4];
hexData[j + 1] = HEX_ARRAY[v & 0x0F];
hexData[j] = HexUtil.HEX_BYTE_ARRAY[v >>> 4];
hexData[j + 1] = HexUtil.HEX_BYTE_ARRAY[v & 0x0F];
}
hexData[length * 2] = '\r';
return hexData;

View File

@ -1,10 +1,12 @@
package com.rusefi.util;
import com.rusefi.io.can.HexUtil;
public class HexBinary {
public static String printHexBinary(byte[] data) {
if (data == null)
return "(null)";
char[] hexCode = "0123456789ABCDEF".toCharArray();
char[] hexCode = HexUtil.HEX_CHAR_ARRAY;
StringBuilder r = new StringBuilder(data.length * 2);
for (byte b : data) {

View File

@ -3,6 +3,7 @@ package com.rusefi.io.can;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class HexUtilTest {
@Test
@ -28,4 +29,9 @@ public class HexUtilTest {
public void unexpected() {
HexUtil.hexToBytes("0xff");
}
@Test
public void asString() {
assertEquals("AB01", HexUtil.asString(new byte[]{(byte) 0xab, 1}));
}
}