Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.hex; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 5 | /* |
| 6 | * Hex converter. |
| 7 | * |
| 8 | * TODO Implement string to byte[] |
| 9 | */ |
| 10 | public class Hex { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 11 | final static char[] HEX = { |
| 12 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 13 | }; |
| 14 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | public final static byte[] toByteArray(String string) { |
| 16 | string = string.trim(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 17 | if ((string.length() & 1) != 0) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | throw new IllegalArgumentException("a hex string must have an even length"); |
| 19 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 20 | byte[] out = new byte[string.length() / 2]; |
| 21 | for (int i = 0; i < out.length; i++) { |
| 22 | int high = nibble(string.charAt(i * 2)) << 4; |
| 23 | int low = nibble(string.charAt(i * 2 + 1)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | out[i] = (byte) (high + low); |
| 25 | } |
| 26 | return out; |
| 27 | } |
| 28 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 29 | public final static int nibble(char c) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | if (c >= '0' && c <= '9') |
| 31 | return c - '0'; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 32 | |
| 33 | if (c >= 'A' && c <= 'F') |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | return c - 'A' + 10; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 35 | if (c >= 'a' && c <= 'f') |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | return c - 'a' + 10; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 37 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | throw new IllegalArgumentException("Not a hex digit: " + c); |
| 39 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 40 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | public final static String toHexString(byte data[]) { |
| 42 | StringBuilder sb = new StringBuilder(); |
| 43 | try { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 44 | append(sb, data); |
| 45 | } |
| 46 | catch (IOException e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | // cannot happen with sb |
| 48 | } |
| 49 | return sb.toString(); |
| 50 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 51 | |
| 52 | public final static void append(Appendable sb, byte[] data) throws IOException { |
| 53 | for (int i = 0; i < data.length; i++) { |
| 54 | sb.append(nibble(data[i] >> 4)); |
| 55 | sb.append(nibble(data[i])); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 59 | private final static char nibble(int i) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | return HEX[i & 0xF]; |
| 61 | } |
| 62 | } |