blob: b04dd046d7e858ba08f526c1b22a868fb7ef403b [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
weibit38c42ed2014-10-09 19:03:54 -070016package org.onlab.util;
17
18public final class HexString {
19
20 private HexString() {
weibit38c42ed2014-10-09 19:03:54 -070021 }
22
23 /**
Charles Chan2340c792015-11-26 19:41:51 -080024 * Convert a byte array to a colon-separated hex string.
weibit38c42ed2014-10-09 19:03:54 -070025 *
Charles Chan2340c792015-11-26 19:41:51 -080026 * @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
weibit38c42ed2014-10-09 19:03:54 -070029 */
30 public static String toHexString(final byte[] bytes) {
Charles Chan2340c792015-11-26 19:41:51 -080031 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 HIGUCHI6b38ee32014-11-14 02:02:59 -080042 if (bytes == null) {
43 return "(null)";
44 }
Charles Chan2340c792015-11-26 19:41:51 -080045 if (separator == null) {
46 separator = "";
47 }
weibit38c42ed2014-10-09 19:03:54 -070048 int i;
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070049 StringBuilder ret = new StringBuilder(bytes.length * 3 - 1);
weibit38c42ed2014-10-09 19:03:54 -070050 String tmp;
51 for (i = 0; i < bytes.length; i++) {
52 if (i > 0) {
Charles Chan2340c792015-11-26 19:41:51 -080053 ret.append(separator);
weibit38c42ed2014-10-09 19:03:54 -070054 }
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 Chan2340c792015-11-26 19:41:51 -080064 /**
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 */
weibit38c42ed2014-10-09 19:03:54 -070072 public static String toHexString(final long val, final int padTo) {
73 char[] arr = Long.toHexString(val).toCharArray();
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070074 StringBuilder ret = new StringBuilder(padTo * 3 - 1);
weibit38c42ed2014-10-09 19:03:54 -070075 // prepend the right number of leading zeros
76 int i = 0;
77 for (; i < (padTo * 2 - arr.length); i++) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070078 ret.append('0');
weibit38c42ed2014-10-09 19:03:54 -070079 if ((i % 2) != 0) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070080 ret.append(':');
weibit38c42ed2014-10-09 19:03:54 -070081 }
82 }
83 for (int j = 0; j < arr.length; j++) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070084 ret.append(arr[j]);
weibit38c42ed2014-10-09 19:03:54 -070085 if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070086 ret.append(':');
weibit38c42ed2014-10-09 19:03:54 -070087 }
88 }
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070089 return ret.toString();
weibit38c42ed2014-10-09 19:03:54 -070090 }
91
Charles Chan2340c792015-11-26 19:41:51 -080092 /**
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 */
weibit38c42ed2014-10-09 19:03:54 -070099 public static String toHexString(final long val) {
100 return toHexString(val, 8);
101 }
102
103 /**
Charles Chan2340c792015-11-26 19:41:51 -0800104 * Convert a colon-separated hex string to byte array.
weibit38c42ed2014-10-09 19:03:54 -0700105 *
Charles Chan2340c792015-11-26 19:41:51 -0800106 * @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
weibit38c42ed2014-10-09 19:03:54 -0700110 */
111 public static byte[] fromHexString(final String values) {
Carmelo Cascone6d9ab3a2016-05-31 11:25:58 -0700112 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);
weibit38c42ed2014-10-09 19:03:54 -0700131 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 Chan2340c792015-11-26 19:41:51 -0800142 /**
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 */
weibit38c42ed2014-10-09 19:03:54 -0700150 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}