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

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

Description

bytes To Hex

License

Open Source License

Declaration

final static String bytesToHex(byte in[]) 

Method Source Code

//package com.java2s;
/**//from   ww  w. j  a v  a 2s.c o  m
 * @copyright Copyright (C) 2014-2015 City of Bloomington, Indiana. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
 * @author W. Sibo <sibow@bloomington.in.gov>
 */

public class Main {
    final static String bytesToHex(byte in[]) {
        byte ch = 0x00;
        int i = 0;
        if (in == null || in.length <= 0)
            return null;
        String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
                "9", "A", "B", "C", "D", "E", "F" };
        StringBuffer out = new StringBuffer(in.length * 2);
        while (i < in.length) {
            ch = (byte) (in[i] & 0xF0); // Strip off high nibble

            ch = (byte) (ch >>> 4);
            // shift the bits down

            ch = (byte) (ch & 0x0F);
            // must do this is high order bit is on!

            out.append(pseudo[(int) ch]); // convert the nibble to a String Character
            ch = (byte) (in[i] & 0x0F); // Strip off low nibble 
            out.append(pseudo[(int) ch]); // convert the nibble to a String Character
            i++;
        }
        String rslt = new String(out);
        return rslt;
    }
}

Related

  1. bytes2HexString(byte[] bytes)
  2. bytes2HexString(byte[] bytes)
  3. bytes2HexString(byte[] src)
  4. bytes2HexStringWithSeparator(String separator, byte... bytes)
  5. bytesToHex(byte bytes[], int offset, int length, boolean wrap)
  6. bytesToHex(byte... bytes)
  7. bytesToHex(byte[] a)
  8. bytesToHex(byte[] a)
  9. bytesToHex(byte[] array)