00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.stream.encoding;
00026
00027
00028
00029
00030
00031
00032
00033 public class HexaEncoding
00034 {
00035
00036
00037
00038
00039
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
00056
00057
00058
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
00077
00078
00079
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
00091
00092
00093
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 }