Java Byte to Hex String byteToHexString(byte num)

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

Description

Converts the given byte value to hex string.

License

Open Source License

Parameter

Parameter Description
num the byte type value to convert.

Return

a string representing given byte value in hexadecimal format.

Declaration

public static String byteToHexString(byte num) 

Method Source Code

//package com.java2s;
/*/*from w w  w . j  a  v  a 2s. c o m*/
 * This file is part of SerialPundit.
 * 
 * Copyright (C) 2014-2016, Rishi Gupta. All rights reserved.
 *
 * The SerialPundit is DUAL LICENSED. It is made available under the terms of the GNU Affero 
 * General Public License (AGPL) v3.0 for non-commercial use and under the terms of a commercial 
 * license for commercial use of this software. 
 * 
 * The SerialPundit 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.
 */

public class Main {
    /**
     * <p>Converts the given byte value to hex string.</p>
     * 
     * @param num the byte type value to convert.
     * @return a string representing given byte value in hexadecimal format.
     */
    public static String byteToHexString(byte num) {
        return toHexString(byteToUnsignedLong(num), '0', 2, 2);
    }

    /**
     * <p>Internal use.</p>
     * 
     * @param num the long number to convert.
     * @param paddingChar the character to use for padding.
     * @param min the minimum length of the resulting String.
     * @param max the maximum length of the resulting String.
     */
    private static String toHexString(final long num, final char paddingChar, int min, int max) {
        /* Formats a long number into the specified length hex String. This is identical to Long.toHexString() 
         * except that it pads (with 0's), or truncates, to the specified size. If max < min, the 
         * functionality is exactly as Long.toHexString(). */
        StringBuffer sb = new StringBuffer(Long.toHexString(num));

        if (max < min) {
            return sb.toString();
        }

        while (sb.length() < max) {
            sb.insert(0, paddingChar);
        }

        return sb.substring(0, min);
    }

    /**
     * <p>Converts the given byte's value to an unsigned long number. The least significant byte (8 bits) of 
     * the long number will be identical to the byte (8 bits) provided, and the most significant 7 bytes 
     * (56 bits) of the long will be zero.</p>
     * 
     * @param data the byte to convert.
     * @return An unsigned long number representing the given byte.
     */
    public static long byteToUnsignedLong(byte data) {
        return 0x00000000000000ff & ((long) data);
    }
}

Related

  1. byteToHexString(byte bytes[])
  2. byteToHexString(byte data)
  3. byteToHexString(byte data)
  4. byteToHexString(byte ib)
  5. byteToHexString(byte in)
  6. byteToHexString(byte src)
  7. byteToHexString(byte value)
  8. byteToHexString(byte value)
  9. byteToHexString(byte[] b)