From 51b7b869d6a6af3c8e2270a50fe7800935b81611 Mon Sep 17 00:00:00 2001 From: SalmanSayyed72 <111107241+SalmanSayyed72@users.noreply.github.com> Date: Sun, 24 Dec 2023 07:21:20 +0000 Subject: [PATCH] Fix:#640 Optimized formatHex method for efficiency and memory usage --- Source/com/drew/metadata/bmp/BmpHeaderDescriptor.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/com/drew/metadata/bmp/BmpHeaderDescriptor.java b/Source/com/drew/metadata/bmp/BmpHeaderDescriptor.java index 5b85905b0..c0a1379b6 100644 --- a/Source/com/drew/metadata/bmp/BmpHeaderDescriptor.java +++ b/Source/com/drew/metadata/bmp/BmpHeaderDescriptor.java @@ -143,7 +143,12 @@ public static String formatHex(@Nullable Long value, int digits) { @NotNull public static String formatHex(long value, int digits) { - return String.format("0x%0"+ digits + "X", value); + StringBuilder sb = new StringBuilder(); + sb.append("0x"); + long maskedValue = value & ((1L << (digits * 4)) - 1); + int leadingZeros = Long.numberOfLeadingZeros(maskedValue) >> 2; + sb.append(Long.toHexString(maskedValue | (1L << (digits * 4))).substring(leadingZeros)); + return sb.toString(); } @Nullable