Added function unhexlate which takes a hex string and returns the byte
array that was used to make it. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1410 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: StringUtil.java,v 1.68 2004/02/25 13:18:39 mdb Exp $
|
||||
// $Id: StringUtil.java,v 1.69 2004/03/04 02:40:23 eric Exp $
|
||||
//
|
||||
// samskivert library - useful routines for java programs
|
||||
// Copyright (C) 2001 Michael Bayne
|
||||
@@ -782,6 +782,31 @@ public class StringUtil
|
||||
return (bytes == null) ? "" : hexlate(bytes, bytes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reproduce the byte array that was used to in the call to hexlate
|
||||
* that resulted in the specified string.
|
||||
*/
|
||||
public static byte[] unhexlate (String hex)
|
||||
{
|
||||
if (hex == null || (hex.length() % 2 != 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// if for some reason we are given a hex string that wasn't made
|
||||
// by hexlate, convert to lowercase so things work.
|
||||
hex = hex.toLowerCase();
|
||||
byte[] data = new byte[hex.length()/2];
|
||||
for (int ii = 0; ii < hex.length(); ii+=2)
|
||||
{
|
||||
int value = (byte)(XLATE.indexOf(hex.charAt(ii)) << 4);
|
||||
value += XLATE.indexOf(hex.charAt(ii+1));
|
||||
|
||||
data[ii/2] = (byte)value;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a hex string representing the MD5 encoded source.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user