mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 07:56:20 +00:00
util/HexDump with space separated hex strings
This commit is contained in:
parent
d15f4d52bb
commit
7aecce7943
@ -20,7 +20,7 @@ import java.security.InvalidParameterException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Clone of Android's /core/java/com/android/internal/util/HexDump class, for use in debugging.
|
* Clone of Android's /core/java/com/android/internal/util/HexDump class, for use in debugging.
|
||||||
* Cosmetic changes only.
|
* Changes: space separated hex strings
|
||||||
*/
|
*/
|
||||||
public class HexDump {
|
public class HexDump {
|
||||||
private final static char[] HEX_DIGITS = {
|
private final static char[] HEX_DIGITS = {
|
||||||
@ -82,10 +82,12 @@ public class HexDump {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String toHexString(byte[] array, int offset, int length) {
|
public static String toHexString(byte[] array, int offset, int length) {
|
||||||
char[] buf = new char[length * 2];
|
char[] buf = new char[length > 0 ? length * 3 - 1 : 0];
|
||||||
|
|
||||||
int bufIndex = 0;
|
int bufIndex = 0;
|
||||||
for (int i = offset; i < offset + length; i++) {
|
for (int i = offset; i < offset + length; i++) {
|
||||||
|
if (i > offset)
|
||||||
|
buf[bufIndex++] = ' ';
|
||||||
byte b = array[i];
|
byte b = array[i];
|
||||||
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
|
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
|
||||||
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
|
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
|
||||||
@ -139,12 +141,13 @@ public class HexDump {
|
|||||||
throw new InvalidParameterException("Invalid hex char '" + c + "'");
|
throw new InvalidParameterException("Invalid hex char '" + c + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** accepts any separator, e.g. space or newline */
|
||||||
public static byte[] hexStringToByteArray(String hexString) {
|
public static byte[] hexStringToByteArray(String hexString) {
|
||||||
int length = hexString.length();
|
int length = hexString.length();
|
||||||
byte[] buffer = new byte[length / 2];
|
byte[] buffer = new byte[(length + 1) / 3];
|
||||||
|
|
||||||
for (int i = 0; i < length; i += 2) {
|
for (int i = 0; i < length; i += 3) {
|
||||||
buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i + 1)));
|
buffer[i / 3] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i + 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user