Java Hex Calculate toHexString(byte[] b)

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

Description

Convenience method to print byte array as hexadecimal string.

License

Open Source License

Parameter

Parameter Description
b Byte array to print.

Return

Hexadecimal string presentation of byte array.

Declaration


public static String toHexString(byte[] b)

    

Method Source Code

//package com.java2s;
/*//from  ww  w  .  ja v  a  2 s  .c o m
    
    
 * Copyright (C) 2006 Softabar
    
    
 * 
    
    
 * This program is free software; you can redistribute it and/or modify it 
    
    
 * under the terms of the GNU General Public License as published by the 
    
    
 * Free Software Foundation; either version 2 of the License, or 
    
    
 * (at your option) any later version.
    
    
 * 
    
    
 * This program 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 General Public License for more details.
    
    
 * 
    
    
 * You should have received a copy of the GNU General Public License 
    
    
 * along with this program; 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[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
            'f' };

    /**
        
        
     * Convenience method to print byte array as hexadecimal string.
        
        
     * @param b Byte array to print.
        
        
     * @return Hexadecimal string presentation of byte array. 
        
        
     */

    public static String toHexString(byte[] b)

    {

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < b.length; i++)

        {

            int c = ((b[i]) >>> 4) & 0xf;

            sb.append(hex[c]);

            c = ((int) b[i] & 0xf);

            sb.append(hex[c]);

        }

        return sb.toString();

    }
}

Related

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