Committed by
Gerrit Code Review
Added possibility to decode HEX strings without separator
Change-Id: I6e11ad2a3dc4e39b148a740d5f19d80705f78b48
Showing
1 changed file
with
19 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++) { | ... | ... |
-
Please register or login to post a comment