Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
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 | |
| 20 | private HexString() { |
| 21 | |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Convert a string of bytes to a ':' separated hex string. |
| 26 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 27 | * @param bytes string of bytes to convert |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 28 | * @return "0f:ca:fe:de:ad:be:ef" |
| 29 | */ |
| 30 | public static String toHexString(final byte[] bytes) { |
Yuta HIGUCHI | 6b38ee3 | 2014-11-14 02:02:59 -0800 | [diff] [blame] | 31 | if (bytes == null) { |
| 32 | return "(null)"; |
| 33 | } |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 34 | int i; |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 35 | StringBuilder ret = new StringBuilder(bytes.length * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 36 | 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 HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 52 | StringBuilder ret = new StringBuilder(padTo * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 53 | // prepend the right number of leading zeros |
| 54 | int i = 0; |
| 55 | for (; i < (padTo * 2 - arr.length); i++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 56 | ret.append('0'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 57 | if ((i % 2) != 0) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 58 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | for (int j = 0; j < arr.length; j++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 62 | ret.append(arr[j]); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 63 | if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 64 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 67 | return ret.toString(); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 68 | } |
| 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 | } |