Java Byte to Hex String byteToHexString(int nib1, int nib2)

Here you can find the source of byteToHexString(int nib1, int nib2)

Description

Converts a single byte into a hexadecimal string.

License

Open Source License

Parameter

Parameter Description
nib1 The first nibble of the byte.
nib2 The second nibble of the byte.

Return

String representation of the hexadecimal representation of a byte.

Declaration

public static String byteToHexString(int nib1, int nib2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** /*from   www .  j av  a  2 s. com*/
     * Converts a single byte into a hexadecimal string.
     *
     * @param nib1 The first nibble of the byte.
     * @param nib2 The second nibble of the byte.
     * @return String representation of the hexadecimal representation of a byte.
     */
    public static String byteToHexString(int nib1, int nib2) {

        char char1, char2;
        char[] chars = new char[2];

        char1 = nibbleToChar(nib1);
        char2 = nibbleToChar(nib2);
        chars[0] = char2;
        chars[1] = char1;

        return (new String(chars));
    }

    /**
     * Converts a nibble into a character.
     *
     * @param nibble The nibble.
     * @return A character representation of the hexadecimal nibble.
     */
    public static char nibbleToChar(int nibble) {

        if (nibble < 10) {
            return (Integer.toString(nibble)).charAt(0);
        } else {
            int nib = nibble - 10;

            return (char) (((char) nib) + 'a');
        }
    }
}

Related

  1. byteToHexString(byte[] data)
  2. byteToHexString(final byte b)
  3. byteToHexString(final byte b)
  4. byteToHexString(final byte inbyte)
  5. byteToHexString(int b)
  6. byteToHexStringPadded(int value)
  7. byteToHexStringSingle(byte[] byteArray)