Committed by
Gerrit Code Review
Improve toHexString such that caller can specify the separator
Change-Id: Ibbcdd2844a4ff5900104f9cd020703bf687bec34
Showing
1 changed file
with
48 additions
and
12 deletions
1 | /* | 1 | /* |
2 | - * Copyright 2014 Open Networking Laboratory | 2 | + * Copyright 2014-2015 Open Networking Laboratory |
3 | * | 3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | 5 | * you may not use this file except in compliance with the License. |
... | @@ -18,25 +18,39 @@ package org.onlab.util; | ... | @@ -18,25 +18,39 @@ package org.onlab.util; |
18 | public final class HexString { | 18 | public final class HexString { |
19 | 19 | ||
20 | private HexString() { | 20 | private HexString() { |
21 | - | ||
22 | } | 21 | } |
23 | 22 | ||
24 | /** | 23 | /** |
25 | - * Convert a string of bytes to a ':' separated hex string. | 24 | + * Convert a byte array to a colon-separated hex string. |
26 | * | 25 | * |
27 | - * @param bytes string of bytes to convert | 26 | + * @param bytes byte array to be converted |
28 | - * @return "0f:ca:fe:de:ad:be:ef" | 27 | + * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef", |
28 | + * or "(null)" if given byte array is null | ||
29 | */ | 29 | */ |
30 | public static String toHexString(final byte[] bytes) { | 30 | public static String toHexString(final byte[] bytes) { |
31 | + return toHexString(bytes, ":"); | ||
32 | + } | ||
33 | + | ||
34 | + /** | ||
35 | + * Convert a byte array to a hex string separated by given separator. | ||
36 | + * | ||
37 | + * @param bytes byte array to be converted | ||
38 | + * @param separator the string use to separate each byte | ||
39 | + * @return converted hex string, or "(null)" if given byte array is null | ||
40 | + */ | ||
41 | + public static String toHexString(final byte[] bytes, String separator) { | ||
31 | if (bytes == null) { | 42 | if (bytes == null) { |
32 | return "(null)"; | 43 | return "(null)"; |
33 | } | 44 | } |
45 | + if (separator == null) { | ||
46 | + separator = ""; | ||
47 | + } | ||
34 | int i; | 48 | int i; |
35 | StringBuilder ret = new StringBuilder(bytes.length * 3 - 1); | 49 | StringBuilder ret = new StringBuilder(bytes.length * 3 - 1); |
36 | String tmp; | 50 | String tmp; |
37 | for (i = 0; i < bytes.length; i++) { | 51 | for (i = 0; i < bytes.length; i++) { |
38 | if (i > 0) { | 52 | if (i > 0) { |
39 | - ret.append(':'); | 53 | + ret.append(separator); |
40 | } | 54 | } |
41 | tmp = Integer.toHexString((bytes[i] & 0xff)); | 55 | tmp = Integer.toHexString((bytes[i] & 0xff)); |
42 | if (tmp.length() == 1) { | 56 | if (tmp.length() == 1) { |
... | @@ -47,6 +61,14 @@ public final class HexString { | ... | @@ -47,6 +61,14 @@ public final class HexString { |
47 | return ret.toString(); | 61 | return ret.toString(); |
48 | } | 62 | } |
49 | 63 | ||
64 | + /** | ||
65 | + * Convert a long number to colon-separated hex string. | ||
66 | + * Prepend zero padding until given length. | ||
67 | + * | ||
68 | + * @param val long number to be converted | ||
69 | + * @param padTo prepend zeros until this length | ||
70 | + * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" | ||
71 | + */ | ||
50 | public static String toHexString(final long val, final int padTo) { | 72 | public static String toHexString(final long val, final int padTo) { |
51 | char[] arr = Long.toHexString(val).toCharArray(); | 73 | char[] arr = Long.toHexString(val).toCharArray(); |
52 | StringBuilder ret = new StringBuilder(padTo * 3 - 1); | 74 | StringBuilder ret = new StringBuilder(padTo * 3 - 1); |
... | @@ -67,18 +89,24 @@ public final class HexString { | ... | @@ -67,18 +89,24 @@ public final class HexString { |
67 | return ret.toString(); | 89 | return ret.toString(); |
68 | } | 90 | } |
69 | 91 | ||
92 | + /** | ||
93 | + * Convert a long number to colon-separated hex string. | ||
94 | + * Prepend zero padding until 8 bytes. | ||
95 | + * | ||
96 | + * @param val long number to be converted | ||
97 | + * @return converted colon-separated hex string, e.g. "0f:ca:fe:de:ad:be:ef" | ||
98 | + */ | ||
70 | public static String toHexString(final long val) { | 99 | public static String toHexString(final long val) { |
71 | return toHexString(val, 8); | 100 | return toHexString(val, 8); |
72 | } | 101 | } |
73 | 102 | ||
74 | /** | 103 | /** |
75 | - * Convert a string of hex values into a string of bytes. | 104 | + * Convert a colon-separated hex string to byte array. |
76 | * | 105 | * |
77 | - * @param values | 106 | + * @param values colon-separated hex string to be converted, |
78 | - * "0f:ca:fe:de:ad:be:ef" | 107 | + * e.g. "0f:ca:fe:de:ad:be:ef" |
79 | - * @return [15, 5 ,2, 5, 17] | 108 | + * @return converted byte array |
80 | - * @throws NumberFormatException | 109 | + * @throws NumberFormatException if input hex string cannot be parsed |
81 | - * If the string can not be parsed | ||
82 | */ | 110 | */ |
83 | public static byte[] fromHexString(final String values) { | 111 | public static byte[] fromHexString(final String values) { |
84 | String[] octets = values.split(":"); | 112 | String[] octets = values.split(":"); |
... | @@ -93,6 +121,14 @@ public final class HexString { | ... | @@ -93,6 +121,14 @@ public final class HexString { |
93 | return ret; | 121 | return ret; |
94 | } | 122 | } |
95 | 123 | ||
124 | + /** | ||
125 | + * Convert a colon-separated hex string to long. | ||
126 | + * | ||
127 | + * @param value colon-separated hex string to be converted, | ||
128 | + * e.g. "00:0f:ca:fe:de:ad:be:ef" | ||
129 | + * @return converted long number | ||
130 | + * @throws NumberFormatException if input hex string cannot be parsed | ||
131 | + */ | ||
96 | public static long toLong(String value) { | 132 | public static long toLong(String value) { |
97 | String[] octets = value.split(":"); | 133 | String[] octets = value.split(":"); |
98 | if (octets.length > 8) { | 134 | if (octets.length > 8) { | ... | ... |
-
Please register or login to post a comment