blob: ddf0f254314dfb0d5c193909885396ef0b9c7004 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001/**
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
18package org.projectfloodlight.openflow.util;
19
20import java.math.BigInteger;
21
22import org.projectfloodlight.openflow.types.U8;
23
24public class HexString {
25 /**
26 * Convert a string of bytes to a ':' separated hex string
27 *
28 * @param bytes
29 * @return "0f:ca:fe:de:ad:be:ef"
30 */
31 public static String toHexString(final byte[] bytes) {
32 int i;
33 String ret = "";
34 String tmp;
35 for (i = 0; i < bytes.length; i++) {
36 if (i > 0)
37 ret += ":";
38 tmp = Integer.toHexString(U8.f(bytes[i]));
39 if (tmp.length() == 1)
40 ret += "0";
41 ret += tmp;
42 }
43 return ret;
44 }
45
46 public static String toHexString(final long val, final int padTo) {
47 char arr[] = Long.toHexString(val).toCharArray();
48 String ret = "";
49 // prepend the right number of leading zeros
50 int i = 0;
51 for (; i < (padTo * 2 - arr.length); i++) {
52 ret += "0";
Andreas Wundsamcfa723f2013-09-24 13:02:46 -070053 if ((i % 2) != 0)
Yotam Harcholf3f11152013-09-05 16:47:16 -070054 ret += ":";
55 }
56 for (int j = 0; j < arr.length; j++) {
57 ret += arr[j];
Andreas Wundsamcfa723f2013-09-24 13:02:46 -070058 if ((((i + j) % 2) != 0) && (j < (arr.length - 1)))
Yotam Harcholf3f11152013-09-05 16:47:16 -070059 ret += ":";
60 }
61 return ret;
62 }
63
64 public static String toHexString(final long val) {
65 return toHexString(val, 8);
66 }
67
68 /**
69 * Convert a string of hex values into a string of bytes
70 *
71 * @param values
72 * "0f:ca:fe:de:ad:be:ef"
73 * @return [15, 5 ,2, 5, 17]
74 * @throws NumberFormatException
75 * If the string can not be parsed
76 */
77 public static byte[] fromHexString(final String values) throws NumberFormatException {
78 String[] octets = values.split(":");
79 byte[] ret = new byte[octets.length];
80
81 for (int i = 0; i < octets.length; i++) {
82 if (octets[i].length() > 2)
83 throw new NumberFormatException("Invalid octet length");
84 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
85 }
86 return ret;
87 }
88
89 public static long toLong(final String values) throws NumberFormatException {
90 // Long.parseLong() can't handle HexStrings with MSB set. Sigh.
91 BigInteger bi = new BigInteger(values.replaceAll(":", ""), 16);
92 if (bi.bitLength() > 64)
93 throw new NumberFormatException("Input string too big to fit in long: "
94 + values);
95 return bi.longValue();
96 }
97
98}