Java Hex Calculate toHexString(byte[] digest)

Here you can find the source of toHexString(byte[] digest)

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte[] digest) 

Method Source Code

//package com.java2s;
/**/*from   w ww  . j a  va 2s .  c o m*/
 * Copyright (c) 2013 Puppet Labs, Inc. and other contributors, as listed below.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Puppet Labs
 */

public class Main {
    private static final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String toHexString(byte[] digest) {
        StringBuilder bld = new StringBuilder();
        appendHex(bld, digest);
        return bld.toString();
    }

    public static void appendHex(StringBuilder bld, byte b) {
        bld.append(hexChars[(b & 0xf0) >> 4]);
        bld.append(hexChars[b & 0x0f]);
    }

    public static void appendHex(StringBuilder bld, byte[] digest) {
        for (int idx = 0; idx < digest.length; ++idx)
            appendHex(bld, digest[idx]);
    }
}

Related

  1. toHexString(byte[] data, int maxLen)
  2. toHexString(byte[] data, int offset, int length)
  3. toHexString(byte[] data, int offset, int length)
  4. toHexString(byte[] data, int start, int len)
  5. toHexString(byte[] data, int start, int length)
  6. toHexString(byte[] digest)
  7. toHexString(byte[] digest)
  8. toHexString(byte[] digestByte)
  9. toHexString(byte[] in)