blob: 3e9ae0386c38f2a0d7718b6ce1bdacea02f5013c [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) {
31 int i;
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070032 StringBuilder ret = new StringBuilder(bytes.length * 3 - 1);
weibit38c42ed2014-10-09 19:03:54 -070033 String tmp;
34 for (i = 0; i < bytes.length; i++) {
35 if (i > 0) {
36 ret.append(':');
37 }
38 tmp = Integer.toHexString((bytes[i] & 0xff));
39 if (tmp.length() == 1) {
40 ret.append('0');
41 }
42 ret.append(tmp);
43 }
44 return ret.toString();
45 }
46
47 public static String toHexString(final long val, final int padTo) {
48 char[] arr = Long.toHexString(val).toCharArray();
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070049 StringBuilder ret = new StringBuilder(padTo * 3 - 1);
weibit38c42ed2014-10-09 19:03:54 -070050 // prepend the right number of leading zeros
51 int i = 0;
52 for (; i < (padTo * 2 - arr.length); i++) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070053 ret.append('0');
weibit38c42ed2014-10-09 19:03:54 -070054 if ((i % 2) != 0) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070055 ret.append(':');
weibit38c42ed2014-10-09 19:03:54 -070056 }
57 }
58 for (int j = 0; j < arr.length; j++) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070059 ret.append(arr[j]);
weibit38c42ed2014-10-09 19:03:54 -070060 if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) {
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070061 ret.append(':');
weibit38c42ed2014-10-09 19:03:54 -070062 }
63 }
Yuta HIGUCHIe5ca93b2014-10-23 09:49:00 -070064 return ret.toString();
weibit38c42ed2014-10-09 19:03:54 -070065 }
66
67 public static String toHexString(final long val) {
68 return toHexString(val, 8);
69 }
70
71 /**
72 * Convert a string of hex values into a string of bytes.
73 *
74 * @param values
75 * "0f:ca:fe:de:ad:be:ef"
76 * @return [15, 5 ,2, 5, 17]
77 * @throws NumberFormatException
78 * If the string can not be parsed
79 */
80 public static byte[] fromHexString(final String values) {
81 String[] octets = values.split(":");
82 byte[] ret = new byte[octets.length];
83
84 for (int i = 0; i < octets.length; i++) {
85 if (octets[i].length() > 2) {
86 throw new NumberFormatException("Invalid octet length");
87 }
88 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
89 }
90 return ret;
91 }
92
93 public static long toLong(String value) {
94 String[] octets = value.split(":");
95 if (octets.length > 8) {
96 throw new NumberFormatException("Input string is too big to fit in long: " + value);
97 }
98 long l = 0;
99 for (String octet: octets) {
100 if (octet.length() > 2) {
101 throw new NumberFormatException(
102 "Each colon-separated byte component must consist of 1 or 2 hex digits: " + value);
103 }
104 short s = Short.parseShort(octet, 16);
105 l = (l << 8) + s;
106 }
107 return l;
108 }
109}