Skip to content

Commit

Permalink
Merge pull request #8 from sniceio/hexdump_lowercase
Browse files Browse the repository at this point in the history
Allowing to dump a string to hex using lowercase alphabet
  • Loading branch information
jonbo372 authored Oct 20, 2023
2 parents 09420b4 + c215e1f commit 613f98c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
51 changes: 45 additions & 6 deletions snice-commons/src/main/java/com/google/polo/pairing/HexDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,39 @@
import java.nio.charset.Charset;

public class HexDump {
private final static char[] HEX_DIGITS = {
private final static char[] HEX_DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

private final static char[] HEX_DIGITS_LOWER_CASE = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

/**
* Dump a byte array as hex, with the option to select whether the ABCDEF hex digits should be
* upper or lower case.
*/
public static String dumpHexString(final byte[] array, boolean upperCase) {
return dumpHexString(array, 0, array.length, upperCase);
}

public static String dumpHexString(final byte[] array) {
return dumpHexString(array, 0, array.length);
return dumpHexString(array, 0, array.length, true);
}

public static String dumpHexString(final byte[] array, final int offset, final int length) {
return dumpHexString(array, offset, length, true);
}

/**
* Dump the given byte array as a hex string, with the option to select whether the ABCDEF hex digits should be
* upper or lower case.
*
* @param array the byte array
* @param offset the offset into the byte array
* @param length the length of the byte array to dump
* @param upperCase flag indicating whether the ABCDEF hex digits should be upper or lower case.
* @return
*/
public static String dumpHexString(final byte[] array, final int offset, final int length, boolean upperCase) {
final StringBuilder result = new StringBuilder();

final byte[] line = new byte[16];
Expand Down Expand Up @@ -84,18 +109,32 @@ public static String toHexString(final byte b) {
}

public static String toHexString(final boolean prefix, final byte[] array) {
return toHexString(prefix, array, 0, array.length);
return toHexString(prefix, array, 0, array.length, true);
}

public static String toHexString(final boolean prefix, final byte[] array, final boolean upperCase) {
return toHexString(prefix, array, 0, array.length, upperCase);
}

public static String toHexString(final byte[] array, final boolean upperCase) {
return toHexString(true, array, 0, array.length, upperCase);
}

public static String toHexString(final byte[] array) {
return toHexString(true, array, 0, array.length);
return toHexString(true, array, 0, array.length, true);
}

public static String toHexString(final byte[] array, final int offset, final int length) {
return toHexString(true, array, offset, length);
}

public static String toHexString(final boolean prefix, final byte[] array, final int offset, final int length) {
return toHexString(prefix, array, offset, length, true);
}

public static String toHexString(final boolean prefix, final byte[] array, final int offset, final int length, boolean upperCase) {
final char[] alphabet = upperCase ? HEX_DIGITS : HEX_DIGITS_LOWER_CASE;

final char[] buf;
int bufIndex = 0;

Expand All @@ -112,8 +151,8 @@ public static String toHexString(final boolean prefix, final byte[] array, final

for (int i = offset; i < (offset + length); i++) {
final byte b = array[i];
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
buf[bufIndex++] = alphabet[(b >>> 4) & 0x0F];
buf[bufIndex++] = alphabet[b & 0x0F];
}

return new String(buf);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.google.polo.pairing;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* @author [email protected]
*/
public class HexDumpTest {

@Test
public void testDumpHexString() {
final var bytes = new byte[] { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, (byte) 0xFF };
assertEquals("0x0A0B0C0D0EFF", HexDump.toHexString(bytes));
assertEquals("0x0a0b0c0d0eff", HexDump.toHexString(bytes, false));
}

}

0 comments on commit 613f98c

Please sign in to comment.