557b848b73aa8e1327f95cc9f6c0c21f77bb6d3a
[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     public static String bytesToHexString(byte[] bytes) {
24         int i;
25         String ret = "";
26         String tmp;
27         StringBuffer buf = new StringBuffer();
28         for (i = 0; i < bytes.length; i++) {
29             if (i > 0)
30                 ret += ":";
31             short u8byte = (short) ((short) bytes[i] & 0xff);
32             tmp = Integer.toHexString(u8byte);
33             if (tmp.length() == 1)
34                 buf.append("0");
35             buf.append(tmp);
36         }
37         ret = buf.toString();
38         return ret;
39     }
40
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
45         int i = 0;
46         for (; i < (16 - arr.length); i++) {
47             buf.append("0");
48             if ((i & 0x01) == 1)
49                 buf.append(":");
50         }
51         for (int j = 0; j < arr.length; j++) {
52             buf.append(arr[j]);
53             if ((((i + j) & 0x01) == 1) && (j < (arr.length - 1)))
54                 buf.append(":");
55         }
56         return buf.toString();
57     }
58
59     public static byte[] bytesFromHexString(String values) {
60         String[] octets = values.split(":");
61         byte[] ret = new byte[octets.length];
62         int i;
63
64         for (i = 0; i < octets.length; i++)
65             ret[i] = Integer.valueOf(octets[i], 16).byteValue();
66         return ret;
67     }
68
69     public static long stringToLong(String values) {
70         long value = new BigInteger(values.replaceAll(":", ""), 16).longValue();
71         return value;
72     }
73
74         /**
75          * This method converts byte array into HexString format with ":" inserted.
76          */
77     public static String bytesToHexStringFormat(byte[] bytes) {
78         int i;
79         String ret = "";
80         String tmp;
81         StringBuffer buf = new StringBuffer();
82         for (i = 0; i < bytes.length; i++) {
83             if (i > 0)
84                 buf.append(":");
85             short u8byte = (short) ((short) bytes[i] & 0xff);
86             tmp = Integer.toHexString(u8byte);
87             if (tmp.length() == 1)
88                 buf.append("0");
89             buf.append(tmp);
90         }
91         ret = buf.toString();
92         return ret;
93     }
94 }