Added possibility to decode HEX strings without separator
Change-Id: I6e11ad2a3dc4e39b148a740d5f19d80705f78b48
(cherry picked from commit 6eaa33ab046dde1ee79ce976457f1fb7f0a85b5f)
diff --git a/utils/misc/src/main/java/org/onlab/util/HexString.java b/utils/misc/src/main/java/org/onlab/util/HexString.java
index 07cf927..b04dd04 100644
--- a/utils/misc/src/main/java/org/onlab/util/HexString.java
+++ b/utils/misc/src/main/java/org/onlab/util/HexString.java
@@ -109,7 +109,25 @@
* @throws NumberFormatException if input hex string cannot be parsed
*/
public static byte[] fromHexString(final String values) {
- String[] octets = values.split(":");
+ return fromHexString(values, ":");
+ }
+
+ /**
+ * Convert a hex-string with arbitrary separator to byte array.
+ * If separator is the empty string or null, then no separator will be considered.
+ *
+ * @param values hex string to be converted
+ * @return converted byte array
+ * @throws NumberFormatException if input hex string cannot be parsed
+ */
+ public static byte[] fromHexString(final String values, String separator) {
+ String regexSeparator;
+ if (separator == null || separator.length() == 0) {
+ regexSeparator = "(?<=\\G.{2})"; // Split string into several two character strings
+ } else {
+ regexSeparator = separator;
+ }
+ String[] octets = values.split(regexSeparator);
byte[] ret = new byte[octets.length];
for (int i = 0; i < octets.length; i++) {