01: /*
02: * Enhydra Java Application Server Project
03: *
04: * The contents of this file are subject to the Enhydra Public License
05: * Version 1.1 (the "License"); you may not use this file except in
06: * compliance with the License. You may obtain a copy of the License on
07: * the Enhydra web site ( http://www.enhydra.org/ ).
08: *
09: * Software distributed under the License is distributed on an "AS IS"
10: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11: * the License for the specific terms governing rights and limitations
12: * under the License.
13: *
14: * The Initial Developer of the Enhydra Application Server is Lutris
15: * Technologies, Inc. The Enhydra Application Server and portions created
16: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17: * All Rights Reserved.
18: *
19: * Contributor(s):
20: *
21: * $Id: HexEncoder.java,v 1.2 2006-06-15 13:47:01 sinisa Exp $
22: */
23:
24: package com.lutris.util;
25:
26: /**
27: * Various conversion methods.
28: * These methods are mostly used to convert internal java data
29: * fields into byte arrays or strings for use in 8 bit ASCII fields.
30: *
31: * @author Mike Ward
32: */
33: public class HexEncoder {
34: /**
35: * Hexadecimal characters corresponding to each half byte value.
36: */
37: private static final char[] HexChars = { '0', '1', '2', '3', '4',
38: '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
39:
40: /**
41: * Converts a long integer to an unsigned hexadecimal String. Treats
42: * the integer as an unsigned 64 bit value and left-pads with the
43: * pad character of the caller's choice.
44: *
45: * @param value The long integer to convert to a hexadecimal string.
46: * @param len The total padded length of the string. If the number
47: * is larger than the padded length, then this length
48: * of the string will be the length of the number.
49: * @param pad The character to use for padding.
50: * @return Unsigned hexadecimal numeric string representing
51: * the specified value.
52: */
53: public static final String toHexString(long value, int len, char pad) {
54: StringBuffer sb = new StringBuffer(Long.toHexString(value));
55: int npad = len - sb.length();
56: while (npad-- > 0)
57: sb.insert(0, pad);
58: return new String(sb);
59: }
60:
61: /**
62: * Converts an arbitrary array of bytes to ASCII hexadecimal string
63: * form, with two hex characters corresponding to each byte. The
64: * length of the resultant string in characters will be twice the
65: * length of the specified array of bytes.
66: *
67: * @param bytes The array of bytes to convert to ASCII hex form.
68: * @return An ASCII hexadecimal numeric string representing the
69: * specified array of bytes.
70: */
71: public static final String toHexString(byte[] bytes) {
72: StringBuffer sb = new StringBuffer();
73: int i;
74: for (i = 0; i < bytes.length; i++) {
75: sb.append(HexChars[(bytes[i] >> 4) & 0xf]);
76: sb.append(HexChars[bytes[i] & 0xf]);
77: }
78: return new String(sb);
79: }
80: }
|