001/*
002 * The contents of this file are subject to the license and copyright detailed
003 * in the LICENSE and NOTICE files at the root of the source tree.
004 */
005package org.duraspace.bagit;
006
007/**
008 * Simple encoder to convert a byte array to bytes.
009 *
010 * From:
011 * https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java
012 * https://github.com/google/guava/blob/master/guava/src/com/google/common/hash/HashCode.java
013 *
014 * If we pull in a dependency which does hex encoding, this can be removed
015 *
016 * @author mikejritter
017 * @since 2020-02-18
018 */
019public class HexEncoder {
020
021    private static final char[] hexDigits = "0123456789abcdef".toCharArray();
022
023    private HexEncoder() {
024    }
025
026    protected static String toString(final byte[] bytes) {
027        final StringBuilder sb = new StringBuilder(2 * bytes.length);
028        for (byte b : bytes) {
029            sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
030        }
031        return sb.toString();
032    }
033}