Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
| 18 | public final class HexString { |
| 19 | |
| 20 | private HexString() { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /** |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 24 | * Convert a byte array to a colon-separated hex string. |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 25 | * |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 26 | * @param bytes byte array to be converted |
| 27 | * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef", |
| 28 | * or "(null)" if given byte array is null |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 29 | */ |
| 30 | public static String toHexString(final byte[] bytes) { |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 31 | return toHexString(bytes, ":"); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Convert a byte array to a hex string separated by given separator. |
| 36 | * |
| 37 | * @param bytes byte array to be converted |
| 38 | * @param separator the string use to separate each byte |
| 39 | * @return converted hex string, or "(null)" if given byte array is null |
| 40 | */ |
| 41 | public static String toHexString(final byte[] bytes, String separator) { |
Yuta HIGUCHI | 6b38ee3 | 2014-11-14 02:02:59 -0800 | [diff] [blame] | 42 | if (bytes == null) { |
| 43 | return "(null)"; |
| 44 | } |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 45 | if (separator == null) { |
| 46 | separator = ""; |
| 47 | } |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 48 | int i; |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 49 | StringBuilder ret = new StringBuilder(bytes.length * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 50 | String tmp; |
| 51 | for (i = 0; i < bytes.length; i++) { |
| 52 | if (i > 0) { |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 53 | ret.append(separator); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 54 | } |
| 55 | tmp = Integer.toHexString((bytes[i] & 0xff)); |
| 56 | if (tmp.length() == 1) { |
| 57 | ret.append('0'); |
| 58 | } |
| 59 | ret.append(tmp); |
| 60 | } |
| 61 | return ret.toString(); |
| 62 | } |
| 63 | |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Convert a long number to colon-separated hex string. |
| 66 | * Prepend zero padding until given length. |
| 67 | * |
| 68 | * @param val long number to be converted |
| 69 | * @param padTo prepend zeros until this length |
| 70 | * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" |
| 71 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 72 | public static String toHexString(final long val, final int padTo) { |
| 73 | char[] arr = Long.toHexString(val).toCharArray(); |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 74 | StringBuilder ret = new StringBuilder(padTo * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 75 | // prepend the right number of leading zeros |
| 76 | int i = 0; |
| 77 | for (; i < (padTo * 2 - arr.length); i++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 78 | ret.append('0'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 79 | if ((i % 2) != 0) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 80 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | for (int j = 0; j < arr.length; j++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 84 | ret.append(arr[j]); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 85 | if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 86 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 89 | return ret.toString(); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Convert a long number to colon-separated hex string. |
| 94 | * Prepend zero padding until 8 bytes. |
| 95 | * |
| 96 | * @param val long number to be converted |
| 97 | * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" |
| 98 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 99 | public static String toHexString(final long val) { |
| 100 | return toHexString(val, 8); |
| 101 | } |
| 102 | |
| 103 | /** |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 104 | * Convert a colon-separated hex string to byte array. |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 105 | * |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 106 | * @param values colon-separated hex string to be converted, |
| 107 | * e.g. "0f:ca:fe:de:ad:be:ef" |
| 108 | * @return converted byte array |
| 109 | * @throws NumberFormatException if input hex string cannot be parsed |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 110 | */ |
| 111 | public static byte[] fromHexString(final String values) { |
Carmelo Cascone | 6eaa33a | 2016-05-31 11:25:58 -0700 | [diff] [blame^] | 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); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 131 | byte[] ret = new byte[octets.length]; |
| 132 | |
| 133 | for (int i = 0; i < octets.length; i++) { |
| 134 | if (octets[i].length() > 2) { |
| 135 | throw new NumberFormatException("Invalid octet length"); |
| 136 | } |
| 137 | ret[i] = Integer.valueOf(octets[i], 16).byteValue(); |
| 138 | } |
| 139 | return ret; |
| 140 | } |
| 141 | |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 142 | /** |
| 143 | * Convert a colon-separated hex string to long. |
| 144 | * |
| 145 | * @param value colon-separated hex string to be converted, |
| 146 | * e.g. "00:0f:ca:fe:de:ad:be:ef" |
| 147 | * @return converted long number |
| 148 | * @throws NumberFormatException if input hex string cannot be parsed |
| 149 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 150 | public static long toLong(String value) { |
| 151 | String[] octets = value.split(":"); |
| 152 | if (octets.length > 8) { |
| 153 | throw new NumberFormatException("Input string is too big to fit in long: " + value); |
| 154 | } |
| 155 | long l = 0; |
| 156 | for (String octet: octets) { |
| 157 | if (octet.length() > 2) { |
| 158 | throw new NumberFormatException( |
| 159 | "Each colon-separated byte component must consist of 1 or 2 hex digits: " + value); |
| 160 | } |
| 161 | short s = Short.parseShort(octet, 16); |
| 162 | l = (l << 8) + s; |
| 163 | } |
| 164 | return l; |
| 165 | } |
| 166 | } |