Fix license header violations in liblldp
[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(byte[] bytes) {
28
29         if (bytes == null) {
30             return "null";
31         }
32
33         String ret = "";
34         StringBuffer buf = new StringBuffer();
35         for (int i = 0; i < bytes.length; i++) {
36             if (i > 0) {
37                 ret += ":";
38             }
39             short u8byte = (short) (bytes[i] & 0xff);
40             String tmp = Integer.toHexString(u8byte);
41             if (tmp.length() == 1) {
42                 buf.append("0");
43             }
44             buf.append(tmp);
45         }
46         ret = buf.toString();
47         return ret;
48     }
49
50     public static String longToHexString(long val) {
51         char arr[] = Long.toHexString(val).toCharArray();
52         StringBuffer buf = new StringBuffer();
53         // prepend the right number of leading zeros
54         int i = 0;
55         for (; i < (16 - arr.length); i++) {
56             buf.append("0");
57             if ((i & 0x01) == 1) {
58                 buf.append(":");
59             }
60         }
61         for (int j = 0; j < arr.length; j++) {
62             buf.append(arr[j]);
63             if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1))) {
64                 buf.append(":");
65             }
66         }
67         return buf.toString();
68     }
69
70
71     public static byte[] bytesFromHexString(String values) {
72         String target = "";
73         if (values != null) {
74             target = values;
75         }
76         String[] octets = target.split(":");
77
78         byte[] ret = new byte[octets.length];
79         for (int i = 0; i < octets.length; i++) {
80             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
81         }
82         return ret;
83     }
84
85     public static long stringToLong(String values) {
86         long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();
87         return value;
88     }
89
90     /**
91      * This method converts byte array into HexString format with ":" inserted.
92      */
93     public static String bytesToHexStringFormat(byte[] bytes) {
94         if (bytes == null) {
95             return "null";
96         }
97         String ret = "";
98         StringBuffer buf = new StringBuffer();
99         for (int i = 0; i < bytes.length; i++) {
100             if (i > 0) {
101                 buf.append(":");
102             }
103             short u8byte = (short) (bytes[i] & 0xff);
104             String tmp = Integer.toHexString(u8byte);
105             if (tmp.length() == 1) {
106                 buf.append("0");
107             }
108             buf.append(tmp);
109         }
110         ret = buf.toString();
111         return ret;
112     }
113 }