From 96c992cd8777e1d3356e3caa69e6ac9a2a5ee5dd Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Sat, 30 Mar 2024 12:38:38 +0100 Subject: [PATCH] BAEL-7703: Update article "Converting Between Byte Arrays and Hexadecimal Strings in Java" (#16271) --- .../algorithms-miscellaneous-5/pom.xml | 12 +++++++ .../conversion/HexStringConverter.java | 21 ++++++++---- .../ByteArrayConverterUnitTest.java | 32 +++++++++++++------ 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/algorithms-modules/algorithms-miscellaneous-5/pom.xml b/algorithms-modules/algorithms-miscellaneous-5/pom.xml index c1739e3690..2d95bf2a01 100644 --- a/algorithms-modules/algorithms-miscellaneous-5/pom.xml +++ b/algorithms-modules/algorithms-miscellaneous-5/pom.xml @@ -5,6 +5,18 @@ 4.0.0 algorithms-miscellaneous-5 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 17 + 17 + + + + algorithms-miscellaneous-5 diff --git a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java index ae434d88ad..5e4b35bfaf 100644 --- a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java +++ b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/conversion/HexStringConverter.java @@ -1,15 +1,12 @@ package com.baeldung.algorithms.conversion; -import java.math.BigInteger; - - - +import com.google.common.io.BaseEncoding; +import jakarta.xml.bind.DatatypeConverter; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; -import com.google.common.io.BaseEncoding; - -import jakarta.xml.bind.DatatypeConverter; +import java.math.BigInteger; +import java.util.HexFormat; public class HexStringConverter { @@ -109,4 +106,14 @@ public class HexStringConverter { return BaseEncoding.base16() .decode(hexString.toUpperCase()); } + + public String encodeUsingHexFormat(byte[] bytes) { + HexFormat hexFormat = HexFormat.of(); + return hexFormat.formatHex(bytes); + } + + public byte[] decodeUsingHexFormat(String hexString) { + HexFormat hexFormat = HexFormat.of(); + return hexFormat.parseHex(hexString); + } } diff --git a/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java b/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java index bb344e8b30..ee003ffac7 100644 --- a/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java +++ b/algorithms-modules/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/conversion/ByteArrayConverterUnitTest.java @@ -1,15 +1,13 @@ package com.baeldung.algorithms.conversion; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - import org.apache.commons.codec.DecoderException; import org.hamcrest.text.IsEqualIgnoringCase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.baeldung.algorithms.conversion.HexStringConverter; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class ByteArrayConverterUnitTest { @@ -24,7 +22,7 @@ class ByteArrayConverterUnitTest { void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() { byte[] bytes = getSampleBytes(); String hexString = getSampleHexString(); - if(hexString.charAt(0) == '0') { + if (hexString.charAt(0) == '0') { hexString = hexString.substring(1); } String output = hexStringConverter.encodeUsingBigIntegerToString(bytes); @@ -46,7 +44,7 @@ class ByteArrayConverterUnitTest { byte[] output = hexStringConverter.decodeUsingBigInteger(hexString); assertArrayEquals(bytes, output); } - + @Test void shouldEncodeByteArrayToHexStringUsingCharacterConversion() { byte[] bytes = getSampleBytes(); @@ -62,7 +60,7 @@ class ByteArrayConverterUnitTest { byte[] output = hexStringConverter.decodeHexString(hexString); assertArrayEquals(bytes, output); } - + @Test void shouldDecodeHexToByteWithInvalidHexCharacter() { assertThrows(IllegalArgumentException.class, () -> { @@ -118,12 +116,28 @@ class ByteArrayConverterUnitTest { assertArrayEquals(bytes, output); } + @Test + void shouldEncodeByteArrayToHexStringUsingHexFormat() throws DecoderException { + byte[] bytes = getSampleBytes(); + String hexString = getSampleHexString(); + String output = hexStringConverter.encodeUsingHexFormat(bytes); + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); + } + + @Test + void shouldDecodeHexStringToByteArrayUsingHexFormat() throws DecoderException { + byte[] bytes = getSampleBytes(); + String hexString = getSampleHexString(); + byte[] output = hexStringConverter.decodeUsingHexFormat(hexString); + assertArrayEquals(bytes, output); + } + private String getSampleHexString() { return "0af50c0e2d10"; } private byte[] getSampleBytes() { - return new byte[] { 10, -11, 12, 14, 45, 16 }; + return new byte[]{10, -11, 12, 14, 45, 16}; } }