ByteArrayHexlifier(): hexlify(), unhexlify(). Fix byteArrayToString()
parent
c516ddf235
commit
8066d1f497
|
@ -1,9 +1,24 @@
|
||||||
package org.hwo;
|
package org.hwo;
|
||||||
|
|
||||||
|
import static org.hwo.logging.Logging.log;
|
||||||
|
import static org.hwo.logging.LogLevel.*;
|
||||||
|
|
||||||
public class ByteArrayHexlifier {
|
public class ByteArrayHexlifier {
|
||||||
|
|
||||||
protected static char[] hexDigits = "0123456789ABCDEF".toCharArray();
|
protected static char[] hexDigits = "0123456789ABCDEF".toCharArray();
|
||||||
|
|
||||||
|
public static String hexlify(String source){
|
||||||
|
String hex = byteArrayToString(source.getBytes());
|
||||||
|
log(DEBUG,"hexlify: %s -> %s",source,hex);
|
||||||
|
return hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String unhexlify(String source){
|
||||||
|
String unhex = new String( stringToByteArray(source) );
|
||||||
|
log(DEBUG,"unhexlify: %s -> %s",source,unhex);
|
||||||
|
return unhex;
|
||||||
|
}
|
||||||
|
|
||||||
public static String byteArrayToString(byte[] bytes)
|
public static String byteArrayToString(byte[] bytes)
|
||||||
{
|
{
|
||||||
char[] target = new char[bytes.length * 2];
|
char[] target = new char[bytes.length * 2];
|
||||||
|
@ -33,19 +48,21 @@ public class ByteArrayHexlifier {
|
||||||
{
|
{
|
||||||
if ((source[2*i] >= 'A')&&(source[2*i] <= 'F'))
|
if ((source[2*i] >= 'A')&&(source[2*i] <= 'F'))
|
||||||
{
|
{
|
||||||
buffer[i] = (byte)(source[2*i] - 'A' + 10);
|
buffer[i] = (byte)((source[2*i] - 'A' + 10)<<4);
|
||||||
} else if ((source[2*i] >= '0')&&(source[2*i] <= '9'))
|
} else if ((source[2*i] >= '0')&&(source[2*i] <= '9'))
|
||||||
{
|
{
|
||||||
buffer[i] = (byte)(source[2*i] - '0');
|
buffer[i] = (byte)((source[2*i] - '0')<<4);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((source[(2*i)+1] >= 'A')&&(source[(2*i)+1] <= 'F'))
|
if ((source[(2*i)+1] >= 'A')&&(source[(2*i)+1] <= 'F'))
|
||||||
{
|
{
|
||||||
buffer[i] |= (byte)((source[(2*i)+1] - 'A' + 10)<<4);
|
buffer[i] |= (byte)((source[(2*i)+1] - 'A' + 10));
|
||||||
} else if ((source[(2*i)+1] >= '0')&&(source[(2*i)+1] <= '9'))
|
} else if ((source[(2*i)+1] >= '0')&&(source[(2*i)+1] <= '9'))
|
||||||
{
|
{
|
||||||
buffer[i] |= (byte)((source[(2*i)+1] - '0')<<4);
|
buffer[i] |= (byte)((source[(2*i)+1] - '0'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log(DEBUG,"buffer[%d] = %d",i,buffer[i]);
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue