Skip to content

Instantly share code, notes, and snippets.

@chexov
Created November 16, 2017 17:04
Show Gist options
  • Save chexov/f4bebccc737b101c38e07a31fc560f63 to your computer and use it in GitHub Desktop.
Save chexov/f4bebccc737b101c38e07a31fc560f63 to your computer and use it in GitHub Desktop.
apache commons-codec class Hex for Android (just put it into your project)
```
Just put it into your code and import like this
import android.org.apache.commons.codec.binary.Hex;
```
package android.org.apache.commons.codec.binary;
import java.nio.charset.Charset;
public class Hex {
public static final Charset DEFAULT_CHARSET;
private static final char[] DIGITS_LOWER;
private static final char[] DIGITS_UPPER;
static {
DEFAULT_CHARSET = Charset.forName("UTF-8");
DIGITS_LOWER = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
DIGITS_UPPER = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
}
public static String encodeHexString(byte[] data) {
return new String(encodeHex(data));
}
public static char[] encodeHex(byte[] data) {
return encodeHex(data, true);
}
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
protected static char[] encodeHex(byte[] data, char[] toDigits) {
int l = data.length;
char[] out = new char[l << 1];
int i = 0;
for (int var5 = 0; i < l; ++i) {
out[var5++] = toDigits[(240 & data[i]) >>> 4];
out[var5++] = toDigits[15 & data[i]];
}
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment