KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > stream > encoding > HexaEncoding


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.stream.encoding;
26
27 /**
28  * This class implements Hexa encoding and decoding
29  *
30  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
31  * @version 1.0
32  */

33 public class HexaEncoding
34 {
35   /**
36    * Convert data into hexa
37    *
38    * @param data to convert
39    * @return the converted string
40    */

41   public static final String JavaDoc data2hex(byte[] data)
42   {
43     if (data == null)
44       return null;
45
46     int len = data.length;
47     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(len * 2);
48     for (int pos = 0; pos < len; pos++)
49       buf.append(toHexChar((data[pos] >>> 4) & 0x0F)).append(
50           toHexChar(data[pos] & 0x0F));
51     return buf.toString();
52   }
53
54   /**
55    * convert hexa into data
56    *
57    * @param str to convert
58    * @return the converted byte array
59    */

60   public static final byte[] hex2data(String JavaDoc str)
61   {
62     if (str == null)
63       return new byte[0];
64
65     int len = str.length();
66     char[] hex = str.toCharArray();
67     byte[] buf = new byte[len / 2];
68
69     for (int pos = 0; pos < len / 2; pos++)
70       buf[pos] = (byte) (((toDataNibble(hex[2 * pos]) << 4) & 0xF0) | (toDataNibble(hex[2 * pos + 1]) & 0x0F));
71
72     return buf;
73   }
74
75   /**
76    * convert value to hexa value
77    *
78    * @param i byte to convert
79    * @return hexa char
80    */

81   public static char toHexChar(int i)
82   {
83     if ((0 <= i) && (i <= 9))
84       return (char) ('0' + i);
85     else
86       return (char) ('a' + (i - 10));
87   }
88
89   /**
90    * convert hexa char to byte value
91    *
92    * @param c hexa character
93    * @return corresponding byte value
94    */

95   public static byte toDataNibble(char c)
96   {
97     if (('0' <= c) && (c <= '9'))
98       return (byte) ((byte) c - (byte) '0');
99     else if (('a' <= c) && (c <= 'f'))
100       return (byte) ((byte) c - (byte) 'a' + 10);
101     else if (('A' <= c) && (c <= 'F'))
102       return (byte) ((byte) c - (byte) 'A' + 10);
103     else
104       return -1;
105   }
106 }
107
Popular Tags