Dumps a single byte into a hex string. - Java java.lang

Java examples for java.lang:byte Array to int

Description

Dumps a single byte into a hex string.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte val = 2;
        System.out.println(dumpByte(val));
    }/*from www.  j  av  a 2s  .  c o  m*/

    /**
     * Dumps a single byte into a hex string.
     *
     * @param val a signed byte
     * @return a byte as hex string
     */
    public static String dumpByte(byte val) {

        return String.format("%02X", byteToInt(val));
    }

    /**
     * Dumps a single byte into a hex string.
     *
     * @param val a unsigned byte
     * @return a byte as hex string
     */
    public static String dumpByte(int val) {

        return String.format("%02X", val);
    }

    /**
     * Converts a unsigned byte to an signed int value.
     *
     * @param b a byte
     * @return the byte as int value
     */
    public static int byteToInt(byte b) {
        return (int) b & 0x00000000FF;
    }
}

Related Tutorials