Java Byte to Hex String byteToHexStr(byte b)

Here you can find the source of byteToHexStr(byte b)

Description

byte To Hex Str

License

Open Source License

Parameter

Parameter Description
b a parameter

Declaration

public static String byteToHexStr(byte b) 

Method Source Code

//package com.java2s;
/*//  w ww .j a  v  a2s  . c o  m
 * This file is part of trolCommander, http://www.trolsoft.ru/soft/trolcommander
 * Copyright (C) 2013-2014 Oleg Trifonov
 *
 * muCommander 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 3 of the License, or
 * (at your option) any later version.
 *
 * muCommander 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final char[] HEX_CHAR_ARRAY = "0123456789ABCDEF".toCharArray();
    private static final String[] HEX_BYTE_STRINGS = new String[256];

    /**
     *
     * @param b
     * @return
     */
    public static String byteToHexStr(byte b) {
        int v = b & 0xFF;
        String result = HEX_BYTE_STRINGS[v];
        if (result == null) {
            result = Character.toString(HEX_CHAR_ARRAY[v >>> 4]) + HEX_CHAR_ARRAY[v & 0x0f];
            HEX_BYTE_STRINGS[v] = result;
        }
        return result;
    }
}

Related

  1. byteToHexStr(byte b)
  2. byteToHexStr(byte buf[])
  3. byteToHexStr(byte src, int len, int code)
  4. byteToHexstr(byte[] b, boolean space)
  5. byteToHexStr(byte[] bArray)