Java Byte Array to Hex String bytesToHexChars(byte[] bytes)

Here you can find the source of bytesToHexChars(byte[] bytes)

Description

bytes To Hex Chars

License

Open Source License

Declaration

public static char[] bytesToHexChars(byte[] bytes) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  .j  a va2 s  . c o  m
  GRANITE DATA SERVICES
  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
    
  This file is part of Granite Data Services.
    
  Granite Data Services is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.
    
  Granite Data Services 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 Library General Public License
  for more details.
    
  You should have received a copy of the GNU Library General Public License
  along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();

    public static char[] bytesToHexChars(byte[] bytes) {
        return bytesToHexChars(bytes, new char[bytes.length * 2], 0);
    }

    public static char[] bytesToHexChars(byte[] bytes, char[] chars, int off) {
        if (chars.length - off < bytes.length * 2)
            throw new IllegalArgumentException("Unsufficient capacity in 'chars' parameter");

        for (int i = off, j = 0; i < bytes.length;) {
            int b = bytes[i++] & 0xFF;
            chars[j++] = HEX_CHARS[b >> 4];
            chars[j++] = HEX_CHARS[b & 0x0F];
        }

        return chars;
    }
}

Related

  1. bytes2hexStr(byte[] arr, int len)
  2. bytes_to_hex(byte[] b)
  3. bytes_to_hex(byte[] bytes)
  4. bytesToHexAppend(byte[] bs, int off, int length, StringBuffer sb)
  5. bytesToHexChars(byte[] bytes)
  6. bytesToHexChars(byte[] bytes)
  7. bytesToHexChecksum(byte[] byteArr)
  8. bytesToHexDelimeter(byte[] data, String delimeter)
  9. bytesToHexFormatted(byte[] bytes)