KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > HexEncoder


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: HexEncoder.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25 package com.lutris.util;
26
27 /**
28  * Various conversion methods.
29  * These methods are mostly used to convert internal java data
30  * fields into byte arrays or strings for use in 8 bit ASCII fields.
31  *
32  * @author Mike Ward
33  */

34 public class HexEncoder {
35     /**
36      * Hexadecimal characters corresponding to each half byte value.
37      */

38     private static final char[] HexChars = {
39     '0', '1', '2', '3', '4', '5', '6', '7',
40     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
41     };
42
43     /**
44      * Converts a long integer to an unsigned hexadecimal String. Treats
45      * the integer as an unsigned 64 bit value and left-pads with the
46      * pad character of the caller's choice.
47      *
48      * @param value The long integer to convert to a hexadecimal string.
49      * @param len The total padded length of the string. If the number
50      * is larger than the padded length, then this length
51      * of the string will be the length of the number.
52      * @param pad The character to use for padding.
53      * @return Unsigned hexadecimal numeric string representing
54      * the specified value.
55      */

56     public static final String JavaDoc toHexString(long value, int len, char pad) {
57     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(Long.toHexString(value));
58     int npad = len - sb.length();
59     while (npad-- > 0) sb.insert(0, pad);
60     return new String JavaDoc(sb);
61     }
62
63     /**
64      * Converts an arbitrary array of bytes to ASCII hexadecimal string
65      * form, with two hex characters corresponding to each byte. The
66      * length of the resultant string in characters will be twice the
67      * length of the specified array of bytes.
68      *
69      * @param bytes The array of bytes to convert to ASCII hex form.
70      * @return An ASCII hexadecimal numeric string representing the
71      * specified array of bytes.
72      */

73     public static final String JavaDoc toHexString(byte[] bytes) {
74     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75     int i;
76     for (i=0; i < bytes.length; i++) {
77         sb.append(HexChars[(bytes[i] >> 4) & 0xf]);
78         sb.append(HexChars[bytes[i] & 0xf]);
79     }
80     return new String JavaDoc(sb);
81     }
82 }
83
84
85
Popular Tags