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) { |
| 31 | int i; |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 32 | StringBuilder ret = new StringBuilder(bytes.length * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 33 | 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 HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 49 | StringBuilder ret = new StringBuilder(padTo * 3 - 1); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 50 | // prepend the right number of leading zeros |
| 51 | int i = 0; |
| 52 | for (; i < (padTo * 2 - arr.length); i++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 53 | ret.append('0'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 54 | if ((i % 2) != 0) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 55 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | for (int j = 0; j < arr.length; j++) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 59 | ret.append(arr[j]); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 60 | if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) { |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 61 | ret.append(':'); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
Yuta HIGUCHI | e5ca93b | 2014-10-23 09:49:00 -0700 | [diff] [blame] | 64 | return ret.toString(); |
weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame] | 65 | } |
| 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 | } |