Java Byte to Hex String byteToHexString(byte b)

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

Description

byte To Hex String

License

Open Source License

Declaration

public static String byteToHexString(byte b) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .jav  a  2s. c o  m*/
 * Copyright (C) 2011 Daniel Ward dwa012@gmail.com
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String byteToHexString(byte b) {
        String result = "";

        byte upperByte = (byte) ((b & 0xf0) >> 4);

        result += byteToChar(upperByte);

        byte lowerByte = (byte) ((b & 0x0f));

        result += byteToChar(lowerByte);

        return result;
    }

    private static char byteToChar(byte c) {
        char result = '0';

        if (c <= 9 && c >= 0) {
            result = (char) (c + '0');
        }

        if (c <= 15 && c >= 10) {
            result = (char) (c - 10 + 'A');
        }

        return result;
    }
}

Related

  1. byteToHexString(byte _b)
  2. byteToHexString(byte b)
  3. byteToHexString(byte b)
  4. byteToHexString(byte b)
  5. byteToHexString(byte b)
  6. byteToHexString(byte b)
  7. byteToHexString(byte b)
  8. byteToHexString(byte b)
  9. byteToHexString(byte bytes[])