3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.utils;
12 import java.math.BigInteger;
15 * The class provides methods to convert hex encode strings
19 public class HexEncode {
21 * This method converts byte array into String format without ":" inserted.
23 public static String bytesToHexString(byte[] bytes) {
27 StringBuffer buf = new StringBuffer();
28 for (i = 0; i < bytes.length; i++) {
31 short u8byte = (short) ((short) bytes[i] & 0xff);
32 tmp = Integer.toHexString(u8byte);
33 if (tmp.length() == 1)
41 public static String longToHexString(long val) {
42 char arr[] = Long.toHexString(val).toCharArray();
43 StringBuffer buf = new StringBuffer();
44 // prepend the right number of leading zeros
46 for (; i < (16 - arr.length); i++) {
51 for (int j = 0; j < arr.length; j++) {
53 if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1)))
56 return buf.toString();
59 public static byte[] bytesFromHexString(String values) {
60 String[] octets = values.split(":");
61 byte[] ret = new byte[octets.length];
64 for (i = 0; i < octets.length; i++)
65 ret[i] = Integer.valueOf(octets[i], 16).byteValue();
69 public static long stringToLong(String values) {
70 long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();
75 * This method converts byte array into HexString format with ":" inserted.
77 public static String bytesToHexStringFormat(byte[] bytes) {
81 StringBuffer buf = new StringBuffer();
82 for (i = 0; i < bytes.length; i++) {
85 short u8byte = (short) ((short) bytes[i] & 0xff);
86 tmp = Integer.toHexString(u8byte);
87 if (tmp.length() == 1)