Added possibility to decode HEX strings without separator
Change-Id: I6e11ad2a3dc4e39b148a740d5f19d80705f78b48 (cherry picked from commit 6eaa33ab)
Showing
2 changed files
with
38 additions
and
1 deletions
... | @@ -109,7 +109,25 @@ public final class HexString { | ... | @@ -109,7 +109,25 @@ public final class HexString { |
109 | * @throws NumberFormatException if input hex string cannot be parsed | 109 | * @throws NumberFormatException if input hex string cannot be parsed |
110 | */ | 110 | */ |
111 | public static byte[] fromHexString(final String values) { | 111 | public static byte[] fromHexString(final String values) { |
112 | - String[] octets = values.split(":"); | 112 | + return fromHexString(values, ":"); |
113 | + } | ||
114 | + | ||
115 | + /** | ||
116 | + * Convert a hex-string with arbitrary separator to byte array. | ||
117 | + * If separator is the empty string or null, then no separator will be considered. | ||
118 | + * | ||
119 | + * @param values hex string to be converted | ||
120 | + * @return converted byte array | ||
121 | + * @throws NumberFormatException if input hex string cannot be parsed | ||
122 | + */ | ||
123 | + public static byte[] fromHexString(final String values, String separator) { | ||
124 | + String regexSeparator; | ||
125 | + if (separator == null || separator.length() == 0) { | ||
126 | + regexSeparator = "(?<=\\G.{2})"; // Split string into several two character strings | ||
127 | + } else { | ||
128 | + regexSeparator = separator; | ||
129 | + } | ||
130 | + String[] octets = values.split(regexSeparator); | ||
113 | byte[] ret = new byte[octets.length]; | 131 | byte[] ret = new byte[octets.length]; |
114 | 132 | ||
115 | for (int i = 0; i < octets.length; i++) { | 133 | for (int i = 0; i < octets.length; i++) { | ... | ... |
... | @@ -21,6 +21,9 @@ import com.esotericsoftware.minlog.Log; | ... | @@ -21,6 +21,9 @@ import com.esotericsoftware.minlog.Log; |
21 | 21 | ||
22 | import junit.framework.TestCase; | 22 | import junit.framework.TestCase; |
23 | 23 | ||
24 | +import java.nio.ByteBuffer; | ||
25 | +import java.util.Arrays; | ||
26 | + | ||
24 | import static org.junit.Assert.fail; | 27 | import static org.junit.Assert.fail; |
25 | 28 | ||
26 | /** | 29 | /** |
... | @@ -55,6 +58,22 @@ public class HexStringTest { | ... | @@ -55,6 +58,22 @@ public class HexStringTest { |
55 | } | 58 | } |
56 | 59 | ||
57 | @Test | 60 | @Test |
61 | + public void testFromHexString() { | ||
62 | + String dpidStr = "3e:1f:01:fc:72:8c:63:31"; | ||
63 | + String dpidStrNoSep = "3e1f01fc728c6331"; | ||
64 | + long valid = 0x3e1f01fc728c6331L; | ||
65 | + byte[] validBytes = ByteBuffer.allocate(Long.BYTES).putLong(valid).array(); | ||
66 | + byte[] testBytes = HexString.fromHexString(dpidStr); | ||
67 | + byte[] testBytesNoSep = HexString.fromHexString(dpidStrNoSep, null); | ||
68 | + byte[] testBytesUCase = HexString.fromHexString(dpidStr.toUpperCase()); | ||
69 | + byte[] testBytesUCaseNoSep = HexString.fromHexString(dpidStrNoSep.toUpperCase(), null); | ||
70 | + TestCase.assertTrue(Arrays.equals(validBytes, testBytes)); | ||
71 | + TestCase.assertTrue(Arrays.equals(validBytes, testBytesNoSep)); | ||
72 | + TestCase.assertTrue(Arrays.equals(validBytes, testBytesUCase)); | ||
73 | + TestCase.assertTrue(Arrays.equals(validBytes, testBytesUCaseNoSep)); | ||
74 | + } | ||
75 | + | ||
76 | + @Test | ||
58 | public void testToLongError() { | 77 | public void testToLongError() { |
59 | String dpidStr = "09:08:07:06:05:04:03:02:01"; | 78 | String dpidStr = "09:08:07:06:05:04:03:02:01"; |
60 | try { | 79 | try { | ... | ... |
-
Please register or login to post a comment