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