Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

HexaEncoding.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.stream.encoding;
00026 
00027 /**
00028  * This class implements Hexa encoding and decoding
00029  * 
00030  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk</a>
00031  * @version 1.0
00032  */
00033 public class HexaEncoding
00034 {
00035   /**
00036    * Convert data into hexa
00037    * 
00038    * @param data to convert
00039    * @return the converted string
00040    */
00041   public static final String data2hex(byte[] data)
00042   {
00043     if (data == null)
00044       return null;
00045 
00046     int len = data.length;
00047     StringBuffer buf = new StringBuffer(len * 2);
00048     for (int pos = 0; pos < len; pos++)
00049       buf.append(toHexChar((data[pos] >>> 4) & 0x0F)).append(
00050           toHexChar(data[pos] & 0x0F));
00051     return buf.toString();
00052   }
00053 
00054   /**
00055    * convert hexa into data
00056    * 
00057    * @param str to convert
00058    * @return the converted byte array
00059    */
00060   public static final byte[] hex2data(String str)
00061   {
00062     if (str == null)
00063       return new byte[0];
00064 
00065     int len = str.length();
00066     char hex[] = str.toCharArray();
00067     byte[] buf = new byte[len / 2];
00068 
00069     for (int pos = 0; pos < len / 2; pos++)
00070       buf[pos] = (byte) (((toDataNibble(hex[2 * pos]) << 4) & 0xF0) | (toDataNibble(hex[2 * pos + 1]) & 0x0F));
00071 
00072     return buf;
00073   }
00074 
00075   /**
00076    * convert value to hexa value
00077    * 
00078    * @param i byte to convert
00079    * @return hexa char
00080    */
00081   public static char toHexChar(int i)
00082   {
00083     if ((0 <= i) && (i <= 9))
00084       return (char) ('0' + i);
00085     else
00086       return (char) ('a' + (i - 10));
00087   }
00088 
00089   /**
00090    * convert hexa char to byte value
00091    * 
00092    * @param c hexa character
00093    * @return corresponding byte value
00094    */
00095   public static byte toDataNibble(char c)
00096   {
00097     if (('0' <= c) && (c <= '9'))
00098       return (byte) ((byte) c - (byte) '0');
00099     else if (('a' <= c) && (c <= 'f'))
00100       return (byte) ((byte) c - (byte) 'a' + 10);
00101     else if (('A' <= c) && (c <= 'F'))
00102       return (byte) ((byte) c - (byte) 'A' + 10);
00103     else
00104       return -1;
00105   }
00106 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1