Fix TransactionContextWrapper limiter accounting
[controller.git] / opendaylight / commons / liblldp / src / main / java / org / opendaylight / controller / liblldp / HexEncode.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.liblldp;
10
11 import java.math.BigInteger;
12
13 /**
14  * The class provides methods to convert hex encode strings
15  *
16  *
17  */
18 public class HexEncode {
19     /**
20      * This method converts byte array into String format without ":" inserted.
21      *
22      * @param bytes
23      *            The byte array to convert to string
24      * @return The hexadecimal representation of the byte array. If bytes is
25      *         null, "null" string is returned
26      */
27     public static String bytesToHexString(final byte[] bytes) {
28
29         if (bytes == null) {
30             return "null";
31         }
32
33         StringBuilder buf = new StringBuilder();
34         for (int i = 0; i < bytes.length; i++) {
35             short u8byte = (short) (bytes[i] & 0xff);
36             String tmp = Integer.toHexString(u8byte);
37             if (tmp.length() == 1) {
38                 buf.append("0");
39             }
40             buf.append(tmp);
41         }
42         return buf.toString();
43     }
44
45     public static String longToHexString(final long val) {
46         char arr[] = Long.toHexString(val).toCharArray();
47         StringBuilder buf = new StringBuilder();
48         // prepend the right number of leading zeros
49         int i = 0;
50         for (; i < (16 - arr.length); i++) {
51             buf.append("0");
52             if ((i & 0x01) == 1) {
53                 buf.append(":");
54             }
55         }
56         for (int j = 0; j < arr.length; j++) {
57             buf.append(arr[j]);
58             if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1))) {
59                 buf.append(":");
60             }
61         }
62         return buf.toString();
63     }
64
65
66     public static byte[] bytesFromHexString(final String values) {
67         String target = "";
68         if (values != null) {
69             target = values;
70         }
71         String[] octets = target.split(":");
72
73         byte[] ret = new byte[octets.length];
74         for (int i = 0; i < octets.length; i++) {
75             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
76         }
77         return ret;
78     }
79
80     public static long stringToLong(final String values) {
81         long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();
82         return value;
83     }
84
85     /**
86      * This method converts byte array into HexString format with ":" inserted.
87      */
88     public static String bytesToHexStringFormat(final byte[] bytes) {
89         if (bytes == null) {
90             return "null";
91         }
92         StringBuilder buf = new StringBuilder();
93         for (int i = 0; i < bytes.length; i++) {
94             if (i > 0) {
95                 buf.append(":");
96             }
97             short u8byte = (short) (bytes[i] & 0xff);
98             String tmp = Integer.toHexString(u8byte);
99             if (tmp.length() == 1) {
100                 buf.append("0");
101             }
102             buf.append(tmp);
103         }
104         return buf.toString();
105     }
106 }