BAEL-7703: Update article "Converting Between Byte Arrays and Hexadecimal Strings in Java" (#16271)

This commit is contained in:
ACHRAF TAITAI 2024-03-30 12:38:38 +01:00 committed by GitHub
parent cc10b9f208
commit 96c992cd87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 49 additions and 16 deletions

View File

@ -5,6 +5,18 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-miscellaneous-5</artifactId> <artifactId>algorithms-miscellaneous-5</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<name>algorithms-miscellaneous-5</name> <name>algorithms-miscellaneous-5</name>
<parent> <parent>

View File

@ -1,15 +1,12 @@
package com.baeldung.algorithms.conversion; 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.DecoderException;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import com.google.common.io.BaseEncoding; import java.math.BigInteger;
import java.util.HexFormat;
import jakarta.xml.bind.DatatypeConverter;
public class HexStringConverter { public class HexStringConverter {
@ -109,4 +106,14 @@ public class HexStringConverter {
return BaseEncoding.base16() return BaseEncoding.base16()
.decode(hexString.toUpperCase()); .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);
}
} }

View File

@ -1,15 +1,13 @@
package com.baeldung.algorithms.conversion; 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.apache.commons.codec.DecoderException;
import org.hamcrest.text.IsEqualIgnoringCase; import org.hamcrest.text.IsEqualIgnoringCase;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 { class ByteArrayConverterUnitTest {
@ -118,6 +116,22 @@ class ByteArrayConverterUnitTest {
assertArrayEquals(bytes, output); 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() { private String getSampleHexString() {
return "0af50c0e2d10"; return "0af50c0e2d10";
} }