blob: a1aba93b85a53c224ae4e2f6199f80a46f595479 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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() {
21
22 }
23
24 /**
25 * Convert a string of bytes to a ':' separated hex string.
26 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080027 * @param bytes string of bytes to convert
weibit38c42ed2014-10-09 19:03:54 -070028 * @return "0f:ca:fe:de:ad:be:ef"
29 */
30 public static String toHexString(final byte[] bytes) {
Yuta HIGUCHI6b38ee32014-11-14 02:02:59 -080031 if (bytes == null) {
32 return "(null)";
33 }
weibit38c42ed2014-10-09 19:03:54 -070034 int i;
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070035 StringBuilder ret = new StringBuilder(bytes.length * 3 - 1);
weibit38c42ed2014-10-09 19:03:54 -070036 String tmp;
37 for (i = 0; i < bytes.length; i++) {
38 if (i > 0) {
39 ret.append(':');
40 }
41 tmp = Integer.toHexString((bytes[i] & 0xff));
42 if (tmp.length() == 1) {
43 ret.append('0');
44 }
45 ret.append(tmp);
46 }
47 return ret.toString();
48 }
49
50 public static String toHexString(final long val, final int padTo) {
51 char[] arr = Long.toHexString(val).toCharArray();
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070052 StringBuilder ret = new StringBuilder(padTo * 3 - 1);
weibit38c42ed2014-10-09 19:03:54 -070053 // prepend the right number of leading zeros
54 int i = 0;
55 for (; i < (padTo * 2 - arr.length); i++) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070056 ret.append('0');
weibit38c42ed2014-10-09 19:03:54 -070057 if ((i % 2) != 0) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070058 ret.append(':');
weibit38c42ed2014-10-09 19:03:54 -070059 }
60 }
61 for (int j = 0; j < arr.length; j++) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070062 ret.append(arr[j]);
weibit38c42ed2014-10-09 19:03:54 -070063 if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070064 ret.append(':');
weibit38c42ed2014-10-09 19:03:54 -070065 }
66 }
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070067 return ret.toString();
weibit38c42ed2014-10-09 19:03:54 -070068 }
69
70 public static String toHexString(final long val) {
71 return toHexString(val, 8);
72 }
73
74 /**
75 * Convert a string of hex values into a string of bytes.
76 *
77 * @param values
78 * "0f:ca:fe:de:ad:be:ef"
79 * @return [15, 5 ,2, 5, 17]
80 * @throws NumberFormatException
81 * If the string can not be parsed
82 */
83 public static byte[] fromHexString(final String values) {
84 String[] octets = values.split(":");
85 byte[] ret = new byte[octets.length];
86
87 for (int i = 0; i < octets.length; i++) {
88 if (octets[i].length() > 2) {
89 throw new NumberFormatException("Invalid octet length");
90 }
91 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
92 }
93 return ret;
94 }
95
96 public static long toLong(String value) {
97 String[] octets = value.split(":");
98 if (octets.length > 8) {
99 throw new NumberFormatException("Input string is too big to fit in long: " + value);
100 }
101 long l = 0;
102 for (String octet: octets) {
103 if (octet.length() > 2) {
104 throw new NumberFormatException(
105 "Each colon-separated byte component must consist of 1 or 2 hex digits: " + value);
106 }
107 short s = Short.parseShort(octet, 16);
108 l = (l << 8) + s;
109 }
110 return l;
111 }
112}