blob: 28eaebdde7a249510b3df2737a21a37dc9b6f6e3 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.pcep.tools;
17
18import javax.xml.bind.DatatypeConverter;
19
20/**
21 * tools fo pcep app.
22 */
23public abstract class PcepTools {
24
25 private PcepTools() {
26
27 }
28
29 /**
30 * Converts decimal byte array to a hex string.
31 *
32 * @param byteArray byte array
33 * @return a hex string
34 */
35 public static String toHexString(byte[] byteArray) {
36 return DatatypeConverter.printHexBinary(byteArray);
37 }
38
39 /**
40 * Converts a hex string to a decimal byte array.
41 *
42 * @param hexString a hex string
43 * @return byte array
44 */
45 public static byte[] toByteArray(String hexString) {
46 return DatatypeConverter.parseHexBinary(hexString);
47 }
48
49 /**
50 * Converts a byte array to a decimal string.
51 *
52 * @param bytes a byte array
53 * @return a decimal string
54 */
55 public static String toDecimalString(byte[] bytes) {
56 String str = "";
57 for (int i = 0; i < bytes.length; i++) {
58 str += String.valueOf(bytes[i]);
59 }
60 return str;
61 }
62
63 /**
64 * convert a string to the form of ip address.
65 *
66 * @param str a string
67 * @return a string with ip format
68 */
69 public static String stringToIp(String str) {
70 long ipInt = Long.parseLong(str, 16);
71 return longToIp(ipInt);
72 }
73
74 /**
75 * convert a long to ip format.
76 *
77 * @param ipLong a decimal number.
78 * @return a ip format string
79 */
80 public static String longToIp(long ipLong) {
81 StringBuilder sb = new StringBuilder();
82 sb.append((ipLong >> 24) & 0xFF).append(".");
83 sb.append((ipLong >> 16) & 0xFF).append(".");
84 sb.append((ipLong >> 8) & 0xFF).append(".");
85 sb.append(ipLong & 0xFF);
86 return sb.toString();
87 }
88
89 /**
90 * convert a string with ip format to a long.
91 *
92 * @param strIp a string with ip format
93 * @return a long number
94 */
95 public static long ipToLong(String strIp) {
96 long[] ip = new long[4];
97 int position1 = strIp.indexOf(".");
98 int position2 = strIp.indexOf(".", position1 + 1);
99 int position3 = strIp.indexOf(".", position2 + 1);
100 ip[0] = Long.parseLong(strIp.substring(0, position1));
101 ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2));
102 ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3));
103 ip[3] = Long.parseLong(strIp.substring(position3 + 1));
104 return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
105 }
106
107 /**
108 * get a integer value from a cut string.
109 *
110 * @param str a whole string
111 * @param base cut the string from this index
112 * @param offset the offset when execute the cut
113 * @return a integer value
114 */
115 public static int tranferHexStringToInt(String str, int base, int offset) {
116 return Integer.parseInt(str.substring(base, offset), 16);
117
118 }
119}