Skip to content

Commit d2ac9c9

Browse files
committed
Slightly better and efficient #toHexString method
1 parent 1985e1a commit d2ac9c9

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,16 @@ public static boolean containsBlanks(final CharSequence s) {
124124
*
125125
* @since 5.0
126126
*/
127+
private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
128+
'e', 'f' };
129+
127130
public static String toHexString(final byte[] bytes) {
128131
if (bytes == null) {
129132
return null;
130133
}
131134
final StringBuilder buffer = new StringBuilder(bytes.length * 2);
132-
for (final byte element : bytes) {
133-
final int unsignedB = element & 0xff;
134-
if (unsignedB < 16) {
135-
buffer.append('0');
136-
}
137-
buffer.append(Integer.toHexString(unsignedB));
135+
for (final byte b : bytes) {
136+
buffer.append(HEX_CHARS[(0xf0 & b) >>> 4]).append(HEX_CHARS[0x0f & b]);
138137
}
139138
return buffer.toString();
140139
}

0 commit comments

Comments
 (0)