From 998f9293403bf515c952ba310c344bb981936f6c Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 2 Mar 2001 01:49:48 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/StringUtil.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 94ed7f0d..79bcd920 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -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; @@ -121,4 +121,26 @@ public class StringUtil 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"; }