FileDocCategorySizeDatePackage
HexEncoder.javaAPI DocAndroid 1.5 API4077Wed May 06 22:41:06 BST 2009org.bouncycastle.util.encoders

HexEncoder

public class HexEncoder extends Object implements Encoder

Fields Summary
protected final byte[]
encodingTable
protected final byte[]
decodingTable
Constructors Summary
public HexEncoder()

        initialiseDecodingTable();
    
Methods Summary
public intdecode(byte[] data, int off, int length, java.io.OutputStream out)
decode the Hex encoded byte data writing it to the given output stream, whitespace characters will be ignored.

return
the number of bytes produced.

        byte    b1, b2;
        int     outLen = 0;
        
        int     end = off + length;
        
        while (end > off)
        {
            if (!ignore((char)data[end - 1]))
            {
                break;
            }
            
            end--;
        }
        
        int i = off;
        while (i < end)
        {
            while (i < end && ignore((char)data[i]))
            {
                i++;
            }
            
            b1 = decodingTable[data[i++]];
            
            while (i < end && ignore((char)data[i]))
            {
                i++;
            }
            
            b2 = decodingTable[data[i++]];

            out.write((b1 << 4) | b2);
            
            outLen++;
        }

        return outLen;
    
public intdecode(java.lang.String data, java.io.OutputStream out)
decode the Hex encoded String data writing it to the given output stream, whitespace characters will be ignored.

return
the number of bytes produced.

        byte    b1, b2;
        int     length = 0;
        
        int     end = data.length();
        
        while (end > 0)
        {
            if (!ignore(data.charAt(end - 1)))
            {
                break;
            }
            
            end--;
        }
        
        int i = 0;
        while (i < end)
        {
            while (i < end && ignore(data.charAt(i)))
            {
                i++;
            }
            
            b1 = decodingTable[data.charAt(i++)];
            
            while (i < end && ignore(data.charAt(i)))
            {
                i++;
            }
            
            b2 = decodingTable[data.charAt(i++)];

            out.write((b1 << 4) | b2);
            
            length++;
        }

        return length;
    
public intencode(byte[] data, int off, int length, java.io.OutputStream out)
encode the input data producing a Hex output stream.

return
the number of bytes produced.

        
        for (int i = off; i < (off + length); i++)
        {
            int    v = data[i] & 0xff;

            out.write(encodingTable[(v >>> 4)]);
            out.write(encodingTable[v & 0xf]);
        }

        return length * 2;
    
private booleanignore(char c)

        return (c == '\n" || c =='\r" || c == '\t" || c == ' ");
    
protected voidinitialiseDecodingTable()


      
    
        for (int i = 0; i < encodingTable.length; i++)
        {
            decodingTable[encodingTable[i]] = (byte)i;
        }
        
        decodingTable['A"] = decodingTable['a"];
        decodingTable['B"] = decodingTable['b"];
        decodingTable['C"] = decodingTable['c"];
        decodingTable['D"] = decodingTable['d"];
        decodingTable['E"] = decodingTable['e"];
        decodingTable['F"] = decodingTable['f"];