Added hexlate() a function that takes an array of bytes and returns a

string that is the hex encoding of said bytes.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@70 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2001-03-02 01:49:48 +00:00
parent de085d0781
commit 998f929340
@@ -1,5 +1,5 @@
// //
// $Id: StringUtil.java,v 1.3 2001/02/13 19:54:43 mdb Exp $ // $Id: StringUtil.java,v 1.4 2001/03/02 01:49:48 mdb Exp $
package com.samskivert.util; package com.samskivert.util;
@@ -121,4 +121,26 @@ public class StringUtil
return buf.toString(); return buf.toString();
} }
/**
* Generates a string from the supplied bytes that is the HEX encoded
* representation of those bytes.
*/
public static String hexlate (byte[] bytes)
{
char[] chars = new char[bytes.length*2];
for (int i = 0; i < chars.length; i++) {
int val = (int)chars[i];
if (val < 0) {
val += 256;
}
chars[2*i] = XLATE.charAt(val/16);
chars[2*i+1] = XLATE.charAt(val%16);
}
return new String(chars);
}
private final static String XLATE = "0123456789abcdef";
} }