KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > stream > encoding > HexaEncoding


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk
20  * Contributor(s): ______________________.
21  */

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

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

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

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

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

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