Java Hex Calculate toHexString(byte[] binaryData)

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

Description

Creates a human-readable hex-String out of a byte-array (e.g.

License

Open Source License

Parameter

Parameter Description
binaryData the data, may be null

Return

the formatted hex string , null if data was null

Declaration

public static String toHexString(byte[] binaryData) 

Method Source Code

//package com.java2s;
/**/*  w  w w. j  a  v a  2s  . co  m*/
   * Tentackle - a framework for java desktop applications
   * Copyright (C) 2001-2008 Harald Krake, harald@krake.de, +49 7722 9508-0
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   */

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

    /**
       * Creates a human-readable hex-String out of a byte-array (e.g. from MessageDigest MD5sum).
       * 
       * @param binaryData the data, may be null
       * @return the formatted hex string , null if data was null
       */
    public static String toHexString(byte[] binaryData) {
        if (binaryData != null) {
            char[] text = new char[binaryData.length << 1];
            int j = 0;
            byte b;

            for (int i = 0; i < binaryData.length; ++i) {
                b = binaryData[i];
                text[j++] = hexDigits[(b & 0xf0) >> 4];
                text[j++] = hexDigits[b & 0x0f];
            }
            return new String(text);
        }
        return null;
    }
}

Related

  1. toHexString(byte[] b, int off, int len)
  2. toHexString(byte[] ba)
  3. toHexString(byte[] ba)
  4. toHexString(byte[] ba, int offset, int length)
  5. toHexString(byte[] binary)
  6. toHexString(byte[] block)
  7. toHexString(byte[] buf)
  8. toHexString(byte[] buf)
  9. toHexString(byte[] buf, int offset, int len)