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