1 package org.openflow.codec.util;
3 import java.math.BigInteger;
5 public class HexString {
7 * Convert a string of bytes to a ':' separated hex string
10 * @return "0f:ca:fe:de:ad:be:ef"
12 public static String toHexString(byte[] bytes) {
16 for (i = 0; i < bytes.length; i++) {
19 tmp = Integer.toHexString(U8.f(bytes[i]));
20 if (tmp.length() == 1)
27 public static String toHexString(long val) {
28 char arr[] = Long.toHexString(val).toCharArray();
30 // prepend the right number of leading zeros
32 for (; i < (16 - arr.length); i++) {
37 for (int j = 0; j < arr.length; j++) {
39 if ((((i + j) % 2) == 1) && (j < (arr.length - 1)))
46 * Convert a string of hex values into a string of bytes
49 * "0f:ca:fe:de:ad:be:ef"
50 * @return [15, 5 ,2, 5, 17]
53 public static byte[] fromHexString(String values) {
54 String[] octets = values.split(":");
55 byte[] ret = new byte[octets.length];
58 for (i = 0; i < octets.length; i++)
59 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
63 public static long toLong(String values) {
64 long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();