blob: 7508b3789941bed1ffd1c3fbb52fedb9e968b496 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
weibit38c42ed2014-10-09 19:03:54 -070019package org.onlab.util;
20
21public final class HexString {
22
23 private HexString() {
24
25 }
26
27 /**
28 * Convert a string of bytes to a ':' separated hex string.
29 *
30 * @param bytes
31 * @return "0f:ca:fe:de:ad:be:ef"
32 */
33 public static String toHexString(final byte[] bytes) {
34 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}