Added a version of hexlate() that only prints out the first N bytes.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@385 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2001-10-18 21:50:18 +00:00
parent 36bcc59ca2
commit 5ef32ff9e2
@@ -1,5 +1,5 @@
// //
// $Id: StringUtil.java,v 1.11 2001/08/15 21:50:03 mdb Exp $ // $Id: StringUtil.java,v 1.12 2001/10/18 21:50:18 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -186,12 +186,17 @@ public class StringUtil
/** /**
* Generates a string from the supplied bytes that is the HEX encoded * Generates a string from the supplied bytes that is the HEX encoded
* representation of those bytes. * representation of those bytes.
*
* @param bytes the bytes for which we want a string representation.
* @param count the number of bytes to stop at (which will be coerced
* into being <= the length of the array).
*/ */
public static String hexlate (byte[] bytes) public static String hexlate (byte[] bytes, int count)
{ {
char[] chars = new char[bytes.length*2]; char[] chars = new char[bytes.length*2];
count = Math.min(count, bytes.length);
for (int i = 0; i < bytes.length; i++) { for (int i = 0; i < count; i++) {
int val = (int)bytes[i]; int val = (int)bytes[i];
if (val < 0) { if (val < 0) {
val += 256; val += 256;
@@ -203,6 +208,15 @@ public class StringUtil
return new String(chars); return new String(chars);
} }
/**
* Generates a string from the supplied bytes that is the HEX encoded
* representation of those bytes.
*/
public static String hexlate (byte[] bytes)
{
return hexlate(bytes, bytes.length);
}
/** /**
* Parses an array of integers from it's string representation. The * Parses an array of integers from it's string representation. The
* array should be represented as a bare list of numbers separated by * array should be represented as a bare list of numbers separated by