1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.openmuc.openiec61850;
 
public class HexConverter {
 
    public static String toHexString(byte b) {
        StringBuilder builder = new StringBuilder();
        appendHexString(b, builder);
        return builder.toString();
    }
 
    public static String toHexString(byte[] bytes) {
        return toHexString(bytes, 0, bytes.length);
    }
 
    public static String toHexString(byte[] bytes, int offset, int length) {
        StringBuilder builder = new StringBuilder();
 
        int l = 1;
        for (int i = offset; i < (offset + length); i++) {
            if ((l != 1) && ((l - 1) % 8 == 0)) {
                builder.append(' ');
            }
            if ((l != 1) && ((l - 1) % 16 == 0)) {
                builder.append('\n');
            }
            l++;
            appendHexString(bytes[i], builder);
            if (i != offset + length - 1) {
                builder.append(' ');
            }
        }
 
        return builder.toString();
    }
 
    /**
     * Returns the integer value as hex string filled with leading zeros. If you do not want leading zeros use
     * Integer.toHexString(int i) instead.
     * 
     * @param i
     *            the integer value to be converted
     * @return the hex string
     */
    public static String toShortHexString(int i) {
        byte[] bytes = new byte[] { (byte) (i >> 24), (byte) (i >> 16), (byte) (i >> 8), (byte) (i) };
        return toShortHexString(bytes);
    }
 
    /**
     * Returns the long value as hex string filled with leading zeros. If you do not want leading zeros use
     * Long.toHexString(long i) instead.
     * 
     * @param l
     *            the long value to be converted
     * @return the hex string
     */
    public static String toShortHexString(long l) {
        byte[] bytes = new byte[] { (byte) (l >> 56), (byte) (l >> 48), (byte) (l >> 40), (byte) (l >> 32),
                (byte) (l >> 24), (byte) (l >> 16), (byte) (l >> 8), (byte) (l) };
        return toShortHexString(bytes);
    }
 
    /**
     * Returns the byte as a hex string. If b is less than 16 the hex string returned contains a leading zero.
     * 
     * @param b
     *            the byte to be converted
     * @return the byte as a hex string.
     */
    public static String toShortHexString(byte b) {
        return toShortHexString(new byte[] { b });
    }
 
    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
 
    public static String toShortHexString(byte[] bytes) {
        return toShortHexString(bytes, 0, bytes.length);
    }
 
    public static String toShortHexString(byte[] bytes, int offset, int length) {
        char[] hexChars = new char[length * 2];
        for (int j = offset; j < (offset + length); j++) {
            int v = bytes[j] & 0xff;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0f];
        }
        return new String(hexChars);
    }
 
    public static byte[] fromShortHexString(String shortHexString) throws NumberFormatException {
 
        validate(shortHexString);
 
        int length = shortHexString.length();
 
        byte[] data = new byte[length / 2];
        for (int i = 0; i < length; i += 2) {
            int firstCharacter = Character.digit(shortHexString.charAt(i), 16);
            int secondCharacter = Character.digit(shortHexString.charAt(i + 1), 16);
 
            if (firstCharacter == -1 || secondCharacter == -1) {
                throw new NumberFormatException("string is not a legal hex string.");
            }
 
            data[i / 2] = (byte) ((firstCharacter << 4) + secondCharacter);
        }
        return data;
    }
 
    public static void appendShortHexString(byte b, StringBuilder builder) {
        builder.append(toShortHexString(b));
    }
 
    public static void appendShortHexString(StringBuilder builder, byte[] bytes, int offset, int length) {
        builder.append(toShortHexString(bytes, offset, length));
    }
 
    public static void appendHexString(byte b, StringBuilder builder) {
        builder.append("0x");
        appendShortHexString(b, builder);
    }
 
    public static void appendHexString(StringBuilder builder, byte[] byteArray, int offset, int length) {
        int l = 1;
        for (int i = offset; i < (offset + length); i++) {
            if ((l != 1) && ((l - 1) % 8 == 0)) {
                builder.append(' ');
            }
            if ((l != 1) && ((l - 1) % 16 == 0)) {
                builder.append('\n');
            }
            l++;
            appendHexString(byteArray[i], builder);
            if (i != offset + length - 1) {
                builder.append(' ');
            }
        }
    }
 
    private static void validate(String s) {
        if (s == null) {
            throw new IllegalArgumentException("string s may not be null");
        }
 
        if ((s.length() == 0) || ((s.length() % 2) != 0)) {
            throw new NumberFormatException("string is not a legal hex string.");
        }
    }
 
    /**
     * Don't let anyone instantiate this class.
     */
    private HexConverter() {
    }
}