You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/06/11 11:00:41 UTC

(httpcomponents-core) branch 5.3.x updated: Slightly better and efficient #toHexString method

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch 5.3.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/5.3.x by this push:
     new d2ac9c9ea Slightly better and efficient #toHexString method
d2ac9c9ea is described below

commit d2ac9c9eabee27aff4a4de4a2b9a832b1f92594f
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Jun 10 09:21:40 2025 +0200

    Slightly better and efficient #toHexString method
---
 .../src/main/java/org/apache/hc/core5/util/TextUtils.java     | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
index 73864f0a0..65c590043 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
@@ -124,17 +124,16 @@ public static boolean containsBlanks(final CharSequence s) {
      *
      * @since 5.0
      */
+    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
+            'e', 'f' };
+
     public static String toHexString(final byte[] bytes) {
         if (bytes == null) {
             return null;
         }
         final StringBuilder buffer = new StringBuilder(bytes.length * 2);
-        for (final byte element : bytes) {
-            final int unsignedB = element & 0xff;
-            if (unsignedB < 16) {
-                buffer.append('0');
-            }
-            buffer.append(Integer.toHexString(unsignedB));
+        for (final byte b : bytes) {
+            buffer.append(HEX_CHARS[(0xf0 & b) >>> 4]).append(HEX_CHARS[0x0f & b]);
         }
         return buffer.toString();
     }