1 package org.openflow.util;
3 import java.math.BigInteger;
5 public class HexString {
7 * Convert a string of bytes to a ':' separated hex string
9 * @return "0f:ca:fe:de:ad:be:ef"
11 public static String toHexString(byte[] bytes) {
15 for(i=0; i< bytes.length; i++) {
18 tmp = Integer.toHexString(U8.f(bytes[i]));
19 if (tmp.length() == 1)
26 public static String toHexString(long val) {
27 char arr[] = Long.toHexString(val).toCharArray();
29 // prepend the right number of leading zeros
31 for (; i < (16 - arr.length); i++) {
36 for (int j = 0; j < arr.length; j++) {
38 if ((((i + j) % 2) == 1) && (j < (arr.length - 1)))
46 * Convert a string of hex values into a string of bytes
47 * @param values "0f:ca:fe:de:ad:be:ef"
48 * @return [15, 5 ,2, 5, 17]
51 public static byte[] fromHexString(String values) {
52 String[] octets = values.split(":");
53 byte[] ret = new byte[octets.length];
56 for(i=0;i<octets.length; i++)
57 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
61 public static long toLong(String values) {
62 long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();