1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import java.text.MessageFormat;
14
15 import org.eclipse.jgit.internal.JGitText;
16
17
18
19
20
21
22 public final class Hex {
23
24 private static final char[] HEX = "0123456789abcdef".toCharArray();
25
26
27 private Hex() {
28
29 }
30
31
32
33
34
35
36
37
38
39
40
41 public static byte[] decode(String s) {
42 int len = s.length();
43 byte[] b = new byte[len / 2];
44
45 for (int i = 0; i < len; i += 2) {
46 int left = Character.digit(s.charAt(i), 16);
47 int right = Character.digit(s.charAt(i + 1), 16);
48
49 if (left == -1 || right == -1) {
50 throw new IllegalArgumentException(MessageFormat.format(
51 JGitText.get().invalidHexString,
52 s));
53 }
54
55 b[i / 2] = (byte) (left << 4 | right);
56 }
57 return b;
58 }
59
60
61
62
63
64
65
66 public static String toHexString(byte[] b) {
67 char[] c = new char[b.length * 2];
68
69 for (int i = 0; i < b.length; i++) {
70 int v = b[i] & 0xFF;
71
72 c[i * 2] = HEX[v >>> 4];
73 c[i * 2 + 1] = HEX[v & 0x0F];
74 }
75
76 return new String(c);
77 }
78 }