2014-07-13 17:11:11 +02:00
|
|
|
package org.hwo;
|
|
|
|
|
2016-10-06 10:54:17 +02:00
|
|
|
import static org.hwo.logging.Logging.log;
|
|
|
|
import static org.hwo.logging.LogLevel.*;
|
|
|
|
|
2014-07-13 17:11:11 +02:00
|
|
|
public class ByteArrayHexlifier {
|
|
|
|
|
|
|
|
protected static char[] hexDigits = "0123456789ABCDEF".toCharArray();
|
|
|
|
|
2016-10-06 10:54:17 +02:00
|
|
|
public static String hexlify(String source){
|
|
|
|
String hex = byteArrayToString(source.getBytes());
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String unhexlify(String source){
|
|
|
|
String unhex = new String( stringToByteArray(source) );
|
|
|
|
return unhex;
|
|
|
|
}
|
|
|
|
|
2014-07-13 17:11:11 +02:00
|
|
|
public static String byteArrayToString(byte[] bytes)
|
|
|
|
{
|
|
|
|
char[] target = new char[bytes.length * 2];
|
|
|
|
|
|
|
|
for (int i=0;i<bytes.length;i++)
|
|
|
|
{
|
2016-04-28 16:24:49 +02:00
|
|
|
target[2*i+1] = hexDigits[bytes[i] & 0x0f];
|
|
|
|
target[(2*i)] = hexDigits[(bytes[i]>>4) & 0x0f];
|
2014-07-13 17:11:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new String(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] stringToByteArray(String hexstring)
|
|
|
|
{
|
|
|
|
byte[] buffer;
|
|
|
|
int len = hexstring.length();
|
|
|
|
|
|
|
|
if ((len % 2)!=0)
|
|
|
|
len--;
|
|
|
|
|
|
|
|
buffer = new byte[ len / 2];
|
|
|
|
|
|
|
|
char[] source = hexstring.toCharArray();
|
|
|
|
|
|
|
|
for (int i=0;i< buffer.length;i++)
|
|
|
|
{
|
|
|
|
if ((source[2*i] >= 'A')&&(source[2*i] <= 'F'))
|
|
|
|
{
|
2016-10-06 10:54:17 +02:00
|
|
|
buffer[i] = (byte)((source[2*i] - 'A' + 10)<<4);
|
2014-07-13 17:11:11 +02:00
|
|
|
} else if ((source[2*i] >= '0')&&(source[2*i] <= '9'))
|
|
|
|
{
|
2016-10-06 10:54:17 +02:00
|
|
|
buffer[i] = (byte)((source[2*i] - '0')<<4);
|
2014-07-13 17:11:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((source[(2*i)+1] >= 'A')&&(source[(2*i)+1] <= 'F'))
|
|
|
|
{
|
2016-10-06 10:54:17 +02:00
|
|
|
buffer[i] |= (byte)((source[(2*i)+1] - 'A' + 10));
|
2014-07-13 17:11:11 +02:00
|
|
|
} else if ((source[(2*i)+1] >= '0')&&(source[(2*i)+1] <= '9'))
|
|
|
|
{
|
2016-10-06 10:54:17 +02:00
|
|
|
buffer[i] |= (byte)((source[(2*i)+1] - '0'));
|
2014-07-13 17:11:11 +02:00
|
|
|
}
|
2016-10-06 10:54:17 +02:00
|
|
|
|
2014-07-13 17:11:11 +02:00
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|