|
| 1 | +package com.baeldung.algorithms.conversion; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | + |
| 5 | +import javax.xml.bind.DatatypeConverter; |
| 6 | + |
| 7 | +import org.apache.commons.codec.DecoderException; |
| 8 | +import org.apache.commons.codec.binary.Hex; |
| 9 | + |
| 10 | +import com.google.common.io.BaseEncoding; |
| 11 | + |
| 12 | +public class HexStringConverter { |
| 13 | + |
| 14 | + /** |
| 15 | + * Create a byte Array from String of hexadecimal digits using Character conversion |
| 16 | + * @param hexString - Hexadecimal digits as String |
| 17 | + * @return Desired byte Array |
| 18 | + */ |
| 19 | + public byte[] decodeHexString(String hexString) { |
| 20 | + if (hexString.length() % 2 == 1) { |
| 21 | + throw new IllegalArgumentException("Invalid hexadecimal String supplied."); |
| 22 | + } |
| 23 | + byte[] bytes = new byte[hexString.length() / 2]; |
| 24 | + |
| 25 | + for (int i = 0; i < hexString.length(); i += 2) { |
| 26 | + bytes[i / 2] = hexToByte(hexString.substring(i, i + 2)); |
| 27 | + } |
| 28 | + return bytes; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Create a String of hexadecimal digits from a byte Array using Character conversion |
| 33 | + * @param byteArray - The byte Array |
| 34 | + * @return Desired String of hexadecimal digits in lower case |
| 35 | + */ |
| 36 | + public String encodeHexString(byte[] byteArray) { |
| 37 | + StringBuffer hexStringBuffer = new StringBuffer(); |
| 38 | + for (int i = 0; i < byteArray.length; i++) { |
| 39 | + hexStringBuffer.append(byteToHex(byteArray[i])); |
| 40 | + } |
| 41 | + return hexStringBuffer.toString(); |
| 42 | + } |
| 43 | + |
| 44 | + public String byteToHex(byte num) { |
| 45 | + char[] hexDigits = new char[2]; |
| 46 | + hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16); |
| 47 | + hexDigits[1] = Character.forDigit((num & 0xF), 16); |
| 48 | + return new String(hexDigits); |
| 49 | + } |
| 50 | + |
| 51 | + public byte hexToByte(String hexString) { |
| 52 | + int firstDigit = toDigit(hexString.charAt(0)); |
| 53 | + int secondDigit = toDigit(hexString.charAt(1)); |
| 54 | + return (byte) ((firstDigit << 4) + secondDigit); |
| 55 | + } |
| 56 | + |
| 57 | + private int toDigit(char hexChar) { |
| 58 | + int digit = Character.digit(hexChar, 16); |
| 59 | + if(digit == -1) { |
| 60 | + throw new IllegalArgumentException("Invalid Hexadecimal Character: "+ hexChar); |
| 61 | + } |
| 62 | + return digit; |
| 63 | + } |
| 64 | + |
| 65 | + public String encodeUsingBigIntegerToString(byte[] bytes) { |
| 66 | + BigInteger bigInteger = new BigInteger(1, bytes); |
| 67 | + return bigInteger.toString(16); |
| 68 | + } |
| 69 | + |
| 70 | + public String encodeUsingBigIntegerStringFormat(byte[] bytes) { |
| 71 | + BigInteger bigInteger = new BigInteger(1, bytes); |
| 72 | + return String.format("%0" + (bytes.length << 1) + "x", bigInteger); |
| 73 | + } |
| 74 | + |
| 75 | + public byte[] decodeUsingBigInteger(String hexString) { |
| 76 | + byte[] byteArray = new BigInteger(hexString, 16).toByteArray(); |
| 77 | + if (byteArray[0] == 0) { |
| 78 | + byte[] output = new byte[byteArray.length - 1]; |
| 79 | + System.arraycopy(byteArray, 1, output, 0, output.length); |
| 80 | + return output; |
| 81 | + } |
| 82 | + return byteArray; |
| 83 | + } |
| 84 | + |
| 85 | + public String encodeUsingDataTypeConverter(byte[] bytes) { |
| 86 | + return DatatypeConverter.printHexBinary(bytes); |
| 87 | + } |
| 88 | + |
| 89 | + public byte[] decodeUsingDataTypeConverter(String hexString) { |
| 90 | + return DatatypeConverter.parseHexBinary(hexString); |
| 91 | + } |
| 92 | + |
| 93 | + public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException { |
| 94 | + return Hex.encodeHexString(bytes); |
| 95 | + } |
| 96 | + |
| 97 | + public byte[] decodeUsingApacheCommons(String hexString) throws DecoderException { |
| 98 | + return Hex.decodeHex(hexString); |
| 99 | + } |
| 100 | + |
| 101 | + public String encodeUsingGuava(byte[] bytes) { |
| 102 | + return BaseEncoding.base16() |
| 103 | + .encode(bytes); |
| 104 | + } |
| 105 | + |
| 106 | + public byte[] decodeUsingGuava(String hexString) { |
| 107 | + return BaseEncoding.base16() |
| 108 | + .decode(hexString.toUpperCase()); |
| 109 | + } |
| 110 | +} |
0 commit comments