On 6/20/2013 8:03 AM, Laura Schmidt wrote:
You have already received several working solutions.
I will strongly recommend you drop the idea about using
BigInteger for this.
It becomes extremely tricky to get it right - you have already
found the sign problem - next problem will be the leading zero
problem.
See below for coding examples (and I am not even sure that I got the
BigInteger hack correct).
Arne
====
import java.math.BigInteger;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
public class ToHex {
private static void test(byte[] ba, ByteArrayHex cvt) {
String s = cvt.encode(ba);
System.out.println(s);
byte[] ba2 = cvt.decode(s);
if(ba2.length != ba.length) {
System.out.println("Length does not match: " + ba2.length + " != " +
ba.length);
}
for(int i = 0; i < Math.min(ba2.length, ba.length); i++) {
if(ba2[i] != ba[i]) {
System.out.println("Byte " + i + " does not match: " + ba2[i] + " !=
" + ba[i]);
}
}
}
private static void testAll(byte[] ba) {
test(ba, new ManualWithInteger());
test(ba, new ManualWithString());
test(ba, new ManualWithCalc());
test(ba, new JAXBTrick());
test(ba, new BigIntegerHack());
}
public static void main(String[] args) {
testAll(new byte[] { 1, 2, 3, 4, 5 });
testAll(new byte[] { -1, -2, -3, -4, -5 });
testAll(new byte[] { 0, 0, 0 });
}
}
interface ByteArrayHex {
public String encode(byte[] ba);
public byte[] decode(String s);
}
class ManualWithInteger implements ByteArrayHex {
public String encode(byte[] ba) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < ba.length; i++) {
sb.append(Integer.toHexString((ba[i] >> 4) & 0x0F));
sb.append(Integer.toHexString(ba[i] & 0x0F));
}
return sb.toString();
}
public byte[] decode(String s) {
int n = s.length() / 2;
byte[] res = new byte[n];
for(int i = 0; i < n; i++) {
res[i] = (byte)(Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16));
}
return res;
}
}
class ManualWithString implements ByteArrayHex {
private static final String HEX = "0123456789abcdef";
public String encode(byte[] ba) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < ba.length; i++) {
sb.append(HEX.charAt((ba[i] >> 4) & 0x0F));
sb.append(HEX.charAt(ba[i] & 0x0F));
}
return sb.toString();
}
public byte[] decode(String s) {
int n = s.length() / 2;
byte[] res = new byte[n];
for(int i = 0; i < n; i++) {
res[i] = (byte)(HEX.indexOf(s.charAt(2 * i)) << 4 |
HEX.indexOf(s.charAt(2 * i + 1)));
}
return res;
}
}
class ManualWithCalc implements ByteArrayHex {
private char encode(int v) {
if(v < 10) {
return (char)('0' + v);
} else if(v < 16) {
return (char)('a' + v - 10);
} else {
throw new RuntimeException("Invalid hex value");
}
}
public String encode(byte[] ba) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < ba.length; i++) {
sb.append(encode((ba[i] >> 4) & 0x0F));
sb.append(encode(ba[i] & 0x0F));
}
return sb.toString();
}
private int decode(char c) {
if('0' <= c && c <= '9') {
return c - '0';
} else if('a' <= c && c <= 'f') {
return c - 'a' + 10;
} else {
throw new RuntimeException("Invalid hex value");
}
}
public byte[] decode(String s) {
int n = s.length() / 2;
byte[] res = new byte[n];
for(int i = 0; i < n; i++) {
res[i] = (byte)(decode(s.charAt(2 * i)) << 4 |
decode(s.charAt(2 * i + 1)));
}
return res;
}
}
class JAXBTrick implements ByteArrayHex {
public String encode(byte[] ba) {
return DatatypeConverter.printHexBinary(ba);
}
public byte[] decode(String s) {
return DatatypeConverter.parseHexBinary(s);
}
}
class BigIntegerHack implements ByteArrayHex {
private String leftPad(String s, char c, int totlen) {
StringBuilder sb = new StringBuilder();
for(int i = s.length(); i < totlen; i++) sb.append(c);
sb.append(s);
return sb.toString();
}
private byte[] leftTrim(byte[] ba, int len) {
return len < ba.length ? Arrays.copyOfRange(ba, ba.length - len,
ba.length) : ba;
}
private byte[] leftPad(byte[] ba, byte b, int totlen) {
byte[] res = new byte[totlen];
int padlen = totlen - ba.length;
for(int i = 0; i < padlen; i++) res[i] = b;
System.arraycopy(ba, 0, res, padlen, ba.length);
return res;
}
public String encode(byte[] ba) {
BigInteger bi = new BigInteger(1, ba);
String s = bi.toString(16);
return leftPad(s, '0', ba.length * 2);
}
public byte[] decode(String s) {
BigInteger bi = new BigInteger(s, 16);
return leftPad(leftTrim(bi.toByteArray(), s.length() / 2), (byte)0,
s.length() / 2);
}
}