blob: 07cc1f71f7c18299812a2fd1c910dee8a255a91c [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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.openflow.util;
19
20import java.math.BigInteger;
21
22public class HexString {
23 /**
24 * Convert a string of bytes to a ':' separated hex string
25 * @param bytes
26 * @return "0f:ca:fe:de:ad:be:ef"
27 */
28 public static String toHexString(byte[] bytes) {
29 int i;
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070030 StringBuilder ret = new StringBuilder(8*2+7);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031 String tmp;
32 for(i=0; i< bytes.length; i++) {
33 if(i> 0)
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070034 ret.append(':');
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080035 tmp = Integer.toHexString(U8.f(bytes[i]));
36 if (tmp.length() == 1)
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070037 ret.append('0');
38 ret.append(tmp);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039 }
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070040 return ret.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041 }
42
43 public static String toHexString(long val, int padTo) {
44 char arr[] = Long.toHexString(val).toCharArray();
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070045 StringBuilder ret = new StringBuilder(8*2+7);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080046 // prepend the right number of leading zeros
47 int i = 0;
48 for (; i < (padTo * 2 - arr.length); i++) {
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070049 ret.append('0');
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080050 if ((i % 2) == 1)
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070051 ret.append(':');
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080052 }
53 for (int j = 0; j < arr.length; j++) {
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070054 ret.append(arr[j]);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055 if ((((i + j) % 2) == 1) && (j < (arr.length - 1)))
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070056 ret.append(':');
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057 }
Yuta HIGUCHI8e8900f2013-10-14 15:56:22 -070058 return ret.toString();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 }
60
61 public static String toHexString(long val) {
62 return toHexString(val, 8);
63 }
64
65
66 /**
67 * Convert a string of hex values into a string of bytes
68 * @param values "0f:ca:fe:de:ad:be:ef"
69 * @return [15, 5 ,2, 5, 17]
70 * @throws NumberFormatException If the string can not be parsed
71 */
72 public static byte[] fromHexString(String values) throws NumberFormatException {
73 String[] octets = values.split(":");
74 byte[] ret = new byte[octets.length];
75
76 for(int i = 0; i < octets.length; i++) {
77 if (octets[i].length() > 2)
78 throw new NumberFormatException("Invalid octet length");
79 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
80 }
81 return ret;
82 }
83
84 public static long toLong(String values) throws NumberFormatException {
85 // Long.parseLong() can't handle HexStrings with MSB set. Sigh.
86 BigInteger bi = new BigInteger(values.replaceAll(":", ""),16);
87 if (bi.bitLength() > 64)
88 throw new NumberFormatException("Input string too big to fit in long: " + values);
89 return bi.longValue();
90 }
91
92}