Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior |
| 3 | * University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package org.projectfloodlight.openflow.util; |
| 19 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 20 | import org.projectfloodlight.openflow.types.U8; |
| 21 | |
| 22 | public class HexString { |
| 23 | /** |
| 24 | * Convert a string of bytes to a ':' separated hex string |
| 25 | * |
| 26 | * @param bytes |
| 27 | * @return "0f:ca:fe:de:ad:be:ef" |
| 28 | */ |
| 29 | public static String toHexString(final byte[] bytes) { |
| 30 | int i; |
| 31 | String ret = ""; |
| 32 | String tmp; |
| 33 | for (i = 0; i < bytes.length; i++) { |
| 34 | if (i > 0) |
| 35 | ret += ":"; |
| 36 | tmp = Integer.toHexString(U8.f(bytes[i])); |
| 37 | if (tmp.length() == 1) |
| 38 | ret += "0"; |
| 39 | ret += tmp; |
| 40 | } |
| 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | public static String toHexString(final long val, final int padTo) { |
| 45 | char arr[] = Long.toHexString(val).toCharArray(); |
| 46 | String ret = ""; |
| 47 | // prepend the right number of leading zeros |
| 48 | int i = 0; |
| 49 | for (; i < (padTo * 2 - arr.length); i++) { |
| 50 | ret += "0"; |
Andreas Wundsam | cfa723f | 2013-09-24 13:02:46 -0700 | [diff] [blame] | 51 | if ((i % 2) != 0) |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 52 | ret += ":"; |
| 53 | } |
| 54 | for (int j = 0; j < arr.length; j++) { |
| 55 | ret += arr[j]; |
Andreas Wundsam | cfa723f | 2013-09-24 13:02:46 -0700 | [diff] [blame] | 56 | if ((((i + j) % 2) != 0) && (j < (arr.length - 1))) |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 57 | ret += ":"; |
| 58 | } |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | public static String toHexString(final long val) { |
| 63 | return toHexString(val, 8); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Convert a string of hex values into a string of bytes |
| 68 | * |
| 69 | * @param values |
| 70 | * "0f:ca:fe:de:ad:be:ef" |
| 71 | * @return [15, 5 ,2, 5, 17] |
| 72 | * @throws NumberFormatException |
| 73 | * If the string can not be parsed |
| 74 | */ |
| 75 | public static byte[] fromHexString(final String values) throws NumberFormatException { |
| 76 | String[] octets = values.split(":"); |
| 77 | byte[] ret = new byte[octets.length]; |
| 78 | |
| 79 | for (int i = 0; i < octets.length; i++) { |
| 80 | if (octets[i].length() > 2) |
| 81 | throw new NumberFormatException("Invalid octet length"); |
| 82 | ret[i] = Integer.valueOf(octets[i], 16).byteValue(); |
| 83 | } |
| 84 | return ret; |
| 85 | } |
| 86 | |
Rob Vaterlaus | a1969e5 | 2014-03-04 16:53:51 -0800 | [diff] [blame] | 87 | public static long toLong(String value) throws NumberFormatException { |
| 88 | String[] octets = value.split(":"); |
| 89 | if (octets.length > 8) |
| 90 | throw new NumberFormatException("Input string is too big to fit in long: " + value); |
| 91 | long l = 0; |
| 92 | for (String octet: octets) { |
| 93 | if (octet.length() > 2) |
| 94 | throw new NumberFormatException("Each colon-separated byte component must consist of 1 or 2 hex digits: " + value); |
Rob Vaterlaus | e36faa3 | 2014-03-04 20:41:09 -0800 | [diff] [blame] | 95 | short s = Short.parseShort(octet, 16); |
Rob Vaterlaus | a1969e5 | 2014-03-04 16:53:51 -0800 | [diff] [blame] | 96 | l = (l << 8) + s; |
| 97 | } |
| 98 | return l; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 99 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 100 | } |