Java Byte Array to Hex bytesToHex(byte[] data)

Here you can find the source of bytesToHex(byte[] data)

Description

This utility method is passed an array of bytes.

License

Open Source License

Declaration

public static String bytesToHex(byte[] data) 

Method Source Code

//package com.java2s;
/*//w w w . j a v a 2  s .  c  om
 *   License
 *
 * The contents of this file are subject to the Jabber Open Source License
 * Version 1.0 (the "License").  You may not copy or use this file, in either
 * source code or executable form, except in compliance with the License.  You
 * may obtain a copy of the License at http://www.jabber.com/license/ or at
 * http://www.opensource.org/.  
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 *   Copyrights
 *
 * Portions created by or assigned to Jabber.com, Inc. are 
 * Copyright (c) 2000 Jabber.com, Inc.  All Rights Reserved.  Contact
 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
 *
 * Portions Copyright (c) 1999-2000 David Waite 
 *
 *   Acknowledgements
 * 
 * Special thanks to the Jabber Open Source Contributors for their
 * suggestions and support of Jabber.
 * 
 *   Changes
 *
 */

public class Main {
    /** quick array to convert byte values to hex codes */
    private final static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };

    /**
     * This utility method is passed an array of bytes. It returns
     * this array as a String in hexadecimal format. This is used
     * internally by <code>digest()</code>. Data is returned in
     * the format specified by the Jabber protocol.
     */
    public static String bytesToHex(byte[] data) {
        StringBuffer retval = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            retval.append(HEX[(data[i] >> 4) & 0x0F]);
            retval.append(HEX[data[i] & 0x0F]);
        }
        return retval.toString();
    }
}

Related

  1. bytesToHex(byte[] bytes)
  2. bytesToHex(byte[] bytes, byte[] hex, int offset)
  3. bytesToHex(byte[] bytes, int groupSize)
  4. bytesToHex(byte[] bytes, int size, char delim)
  5. bytesToHex(byte[] data)
  6. bytesToHex(byte[] data)
  7. bytesToHex(byte[] data)
  8. bytesToHex(byte[] data)
  9. bytesToHex(byte[] data, char[] chars)