1
package org.bouncycastle.util.encoders;
3
import java.io.IOException;
4
import java.io.OutputStream;
6
public class HexEncoder
9
protected final byte[] encodingTable =
11
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
12
(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
16
* set up the decoding table.
18
protected final byte[] decodingTable = new byte[128];
20
protected void initialiseDecodingTable()
22
for (int i = 0; i < encodingTable.length; i++)
24
decodingTable[encodingTable[i]] = (byte)i;
27
decodingTable['A'] = decodingTable['a'];
28
decodingTable['B'] = decodingTable['b'];
29
decodingTable['C'] = decodingTable['c'];
30
decodingTable['D'] = decodingTable['d'];
31
decodingTable['E'] = decodingTable['e'];
32
decodingTable['F'] = decodingTable['f'];
37
initialiseDecodingTable();
41
* encode the input data producing a Hex output stream.
43
* @return the number of bytes produced.
52
for (int i = off; i < (off + length); i++)
54
int v = data[i] & 0xff;
56
out.write(encodingTable[(v >>> 4)]);
57
out.write(encodingTable[v & 0xf]);
63
private boolean ignore(
66
return (c == '\n' || c =='\r' || c == '\t' || c == ' ');
70
* decode the Hex encoded byte data writing it to the given output stream,
71
* whitespace characters will be ignored.
73
* @return the number of bytes produced.
85
int end = off + length;
89
if (!ignore((char)data[end - 1]))
100
while (i < end && ignore((char)data[i]))
105
b1 = decodingTable[data[i++]];
107
while (i < end && ignore((char)data[i]))
112
b2 = decodingTable[data[i++]];
114
out.write((b1 << 4) | b2);
123
* decode the Hex encoded String data writing it to the given output stream,
124
* whitespace characters will be ignored.
126
* @return the number of bytes produced.
136
int end = data.length();
140
if (!ignore(data.charAt(end - 1)))
151
while (i < end && ignore(data.charAt(i)))
156
b1 = decodingTable[data.charAt(i++)];
158
while (i < end && ignore(data.charAt(i)))
163
b2 = decodingTable[data.charAt(i++)];
165
out.write((b1 << 4) | b2);