FileDocCategorySizeDatePackage
Codec.javaAPI DocApache Poi 3.0.17266Mon Jan 01 12:39:34 GMT 2007org.apache.poi.contrib.poibrowser

Codec

public class Codec extends Object

Provides utility methods for encoding and decoding hexadecimal data.

author
Rainer Klute (klute@rainer-klute.de) - with portions from Tomcat
version
$Id: Codec.java 489730 2006-12-22 19:18:16Z bayard $
since
2002-01-24

Fields Summary
protected static final byte[]
hexval

The nibbles' hexadecimal values. A nibble is a half byte.

Constructors Summary
Methods Summary
protected static bytedecodeNibble(char c)

Decodes a nibble.

param
c A character in the range '0'-'9' or 'A'-'F'. Lower case is not supported here.
return
The decoded nibble in the range 0-15
throws
IllegalArgumentException if c is not a permitted character

        for (byte i = 0; i < hexval.length; i++)
            if ((byte) c == hexval[i])
                return i;
        throw new IllegalArgumentException("\"" + c + "\"" +
                                           " does not represent a nibble.");
    
public static byte[]hexDecode(java.lang.String s)

Decodes the hexadecimal representation of a sequence of bytes into a byte array. Each character in the string represents a nibble (half byte) and must be one of the characters '0'-'9', 'A'-'F' or 'a'-'f'.

param
s The string to be decoded
return
The bytes
throws
IllegalArgumentException if the string does not contain a valid representation of a byte sequence.

        final int length = s.length();

        /* The string to be converted must have an even number of
           characters. */
        if (length % 2 == 1)
            throw new IllegalArgumentException
                ("String has odd length " + length);
        byte[] b = new byte[length / 2];
        char[] c = new char[length];
        s.toUpperCase().getChars(0, length, c, 0);
        for (int i = 0; i < length; i += 2)
            b[i/2] = (byte) (decodeNibble(c[i]) << 4 & 0xF0 |
                             decodeNibble(c[i+1])    & 0x0F);
        return b;
    
public static java.lang.StringhexEncode(java.lang.String s)

Converts a string into its hexadecimal notation.




                
         
    
        return hexEncode(s.getBytes());
    
public static java.lang.StringhexEncode(byte[] s)

Converts a byte array into its hexadecimal notation.

        return hexEncode(s, 0, s.length);
    
public static java.lang.StringhexEncode(byte[] s, int offset, int length)

Converts a part of a byte array into its hexadecimal notation.

        StringBuffer b = new StringBuffer(length * 2);
        for (int i = offset; i < offset + length; i++)
        {
            int c = s[i];
            b.append((char) hexval[(c & 0xF0) >> 4]);
            b.append((char) hexval[(c & 0x0F) >> 0]);
        }
        return b.toString();
    
public static java.lang.StringhexEncode(byte b)

Converts a single byte into its hexadecimal notation.

        StringBuffer sb = new StringBuffer(2);
        sb.append((char) hexval[(b & 0xF0) >> 4]);
        sb.append((char) hexval[(b & 0x0F) >> 0]);
        return sb.toString();
    
public static java.lang.StringhexEncode(short s)

Converts a short value (16-bit) into its hexadecimal notation.

        StringBuffer sb = new StringBuffer(4);
        sb.append((char) hexval[(s & 0xF000) >> 12]);
        sb.append((char) hexval[(s & 0x0F00) >>  8]);
        sb.append((char) hexval[(s & 0x00F0) >>  4]);
        sb.append((char) hexval[(s & 0x000F) >>  0]);
        return sb.toString();
    
public static java.lang.StringhexEncode(int i)

Converts an int value (32-bit) into its hexadecimal notation.

        StringBuffer sb = new StringBuffer(8);
        sb.append((char) hexval[(i & 0xF0000000) >> 28]);
        sb.append((char) hexval[(i & 0x0F000000) >> 24]);
        sb.append((char) hexval[(i & 0x00F00000) >> 20]);
        sb.append((char) hexval[(i & 0x000F0000) >> 16]);
        sb.append((char) hexval[(i & 0x0000F000) >> 12]);
        sb.append((char) hexval[(i & 0x00000F00) >>  8]);
        sb.append((char) hexval[(i & 0x000000F0) >>  4]);
        sb.append((char) hexval[(i & 0x0000000F) >>  0]);
        return sb.toString();
    
public static java.lang.StringhexEncode(long l)

Converts a long value (64-bit) into its hexadecimal notation.

        StringBuffer sb = new StringBuffer(16);
        sb.append(hexEncode((int) (l & 0xFFFFFFFF00000000L) >> 32));
        sb.append(hexEncode((int) (l & 0x00000000FFFFFFFFL) >>  0));
        return sb.toString();
    
public static java.lang.StringhexEncode(org.apache.poi.hpsf.ClassID classID)

Converts a class ID into its hexadecimal notation.

        return hexEncode(classID.getBytes());
    
public static voidmain(java.lang.String[] args)

For testing.

        final BufferedReader in =
            new BufferedReader(new InputStreamReader(System.in));
        String s;
        do
        {
            s = in.readLine();
            if (s != null)
            {
                String bytes = hexEncode(s);
                System.out.print("Hex encoded (String): ");
                System.out.println(bytes);
                System.out.print("Hex encoded (byte[]): ");
                System.out.println(hexEncode(s.getBytes()));
                System.out.print("Re-decoded (byte[]):  ");
                System.out.println(new String(hexDecode(bytes)));
            }
        }
        while (s != null);