Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 20 | private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); |
| 21 | |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 22 | private HexString() { |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /** |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 26 | * Convert a byte array to a colon-separated hex string. |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 27 | * |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 28 | * @param bytes byte array to be converted |
| 29 | * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef", |
| 30 | * or "(null)" if given byte array is null |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 31 | */ |
| 32 | public static String toHexString(final byte[] bytes) { |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 33 | return toHexString(bytes, ":"); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Convert a byte array to a hex string separated by given separator. |
| 38 | * |
| 39 | * @param bytes byte array to be converted |
| 40 | * @param separator the string use to separate each byte |
| 41 | * @return converted hex string, or "(null)" if given byte array is null |
| 42 | */ |
| 43 | public static String toHexString(final byte[] bytes, String separator) { |
Yuta HIGUCHI | 6b38ee3 | 2014-11-14 02:02:59 -0800 | [diff] [blame] | 44 | if (bytes == null) { |
| 45 | return "(null)"; |
| 46 | } |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 47 | if (separator == null) { |
| 48 | separator = ""; |
| 49 | } |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 50 | int slen = bytes.length * (2 + separator.length()) - separator.length(); |
| 51 | StringBuilder ret = new StringBuilder(slen); |
| 52 | boolean addSeparator = !separator.isEmpty(); |
| 53 | for (int i = 0; i < bytes.length; i++) { |
| 54 | if (i > 0 && addSeparator) { |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 55 | ret.append(separator); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 56 | } |
Yuta HIGUCHI | 6584989 | 2017-08-18 13:09:41 -0700 | [diff] [blame] | 57 | ret.append(HEX_CHARS[(bytes[i] >> 4) & 0xF]); |
| 58 | ret.append(HEX_CHARS[(bytes[i] & 0xF)]); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 59 | } |
| 60 | return ret.toString(); |
| 61 | } |
| 62 | |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Convert a long number to colon-separated hex string. |
| 65 | * Prepend zero padding until given length. |
| 66 | * |
| 67 | * @param val long number to be converted |
| 68 | * @param padTo prepend zeros until this length |
| 69 | * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" |
| 70 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 71 | public static String toHexString(final long val, final int padTo) { |
| 72 | char[] arr = Long.toHexString(val).toCharArray(); |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 73 | StringBuilder ret = new StringBuilder(padTo * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 74 | // prepend the right number of leading zeros |
| 75 | int i = 0; |
| 76 | for (; i < (padTo * 2 - arr.length); i++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 77 | ret.append('0'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 78 | if ((i % 2) != 0) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 79 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | for (int j = 0; j < arr.length; j++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 83 | ret.append(arr[j]); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 84 | if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 85 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 88 | return ret.toString(); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Convert a long number to colon-separated hex string. |
| 93 | * Prepend zero padding until 8 bytes. |
| 94 | * |
| 95 | * @param val long number to be converted |
| 96 | * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" |
| 97 | */ |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 98 | public static String toHexString(final long val) { |
| 99 | return toHexString(val, 8); |
| 100 | } |
| 101 | |
| 102 | /** |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 103 | * Convert a colon-separated hex string to byte array. |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 104 | * |
Charles Chan | 2340c79 | 2015-11-26 19:41:51 -0800 | [diff] [blame] | 105 | * @param values colon-separated hex string to be converted, |
| 106 | * e.g. "0f:ca:fe:de:ad:be:ef" |
| 107 | * @return converted byte array |
| 108 | * @throws NumberFormatException if input hex string cannot be parsed |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 109 | */ |
| 110 | public static byte[] fromHexString(final String values) { |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 111 | return fromHexString(values, ":"); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Convert a hex-string with arbitrary separator to byte array. |
| 116 | * If separator is the empty string or null, then no separator will be considered. |
| 117 | * |
| 118 | * @param values hex string to be converted |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 119 | * @param separator regex for separator |
Carmelo Cascone | 6d9ab3a | 2016-05-31 11:25:58 -0700 | [diff] [blame] | 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 | } |