Java Hex Calculate toHexString(final byte value)

Here you can find the source of toHexString(final byte value)

Description

Returns a unsigned hex String of the byte value.

License

Open Source License

Parameter

Parameter Description
value The <code>byte</code> value

Return

the value as unsigned hex

Declaration

public static String toHexString(final byte value) 

Method Source Code

//package com.java2s;
/**/*from   w w w  . j  a va  2 s.c o m*/
 * jModuleConnect is an framework for communication and file management on modem 
 * modules.
 * 
 * This project was inspired by the project TC65SH 
 * by Christoph Vilsmeier: <http://www.vilsmeier-consulting.de/tc65sh.html>
 * 
 * Copyright (C) 2015 sitec systems GmbH <http://www.sitec-systems.de>
 * 
 * This file is part of jModuleConnect.
 * 
 * jModuleConnect is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * jModuleConnect 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 Lesser General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with jModuleConnect. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Standard mask for binary to interpret as unsigned
     */
    private final static int MASK = 0xFF;

    /**
     * Returns a unsigned hex <code>String</code> of the <code>byte</code> value.
     * @param value The <code>byte</code> value
     * @return the value as unsigned hex
     * @since 1.0
     */
    public static String toHexString(final byte value) {
        String result = Integer.toHexString(toUnsigned(value)).toUpperCase();

        if (result.length() == 1) {
            result = "0x0" + result;
        } else {
            result = "0x" + result;
        }

        return result;
    }

    /**
     * Returns a <code>String</code> with unsigned hex representation of the input
     * <code>byte[]</code>.
     * @param array The <code>byte[]</code>
     * @return The <code>byte[]</code> as unsigned hex <code>String</code>
     * @since 1.0
     */
    public static String toHexString(final byte[] array) {
        nullCheck(array, "array");

        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            sb.append("[").append(toHexString(array[i])).append("]");
        }

        return sb.toString();
    }

    /**
     * Converts a <code>byte</code> to a unsigned <code>short</code> value
     * between 0 - 255.
     * @param value the <code>byte</code> value
     * @return the value as unsigned
     * @since 1.0
     */
    public static short toUnsigned(final byte value) {
        return (short) (value & MASK);
    }

    /**
     * Converts a <code>short</code> to a unsigned <code>int</code> value
     * between 0 - 65535.
     * @param value the <code>short</code> value
     * @return the value as unsigned
     * @since 1.0
     */
    public static int toUnsigned(final short value) {
        return value & 0xFFFF;
    }

    /**
     * Converts a <code>int</code> to a unsigned <code>long</code> value
     * between 0 - 4294967295.
     * @param value the <code>int</code> value
     * @return the value as unsigned
     * @since 1.0
     */
    public static long toUnsigned(final int value) {
        return (long) (value & 0x00000000FFFFFFFFL);
    }

    private static void nullCheck(final byte[] buffer, final String parameter) {
        if (buffer == null) {
            throw new NullPointerException("Parameter " + parameter + " cant be null");
        }
    }
}

Related

  1. toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase, char separator)
  2. toHexString(char c)
  3. toHexString(final byte b)
  4. toHexString(final byte b)
  5. toHexString(final byte hex)
  6. toHexString(final byte[] arr)
  7. toHexString(final byte[] array)
  8. toHexString(final byte[] b)
  9. toHexString(final byte[] bs)