Java Byte Create toByteValue(final int value)

Here you can find the source of toByteValue(final int value)

Description

Converts a signed or unsigned 8-bit integer into a byte.

License

Apache License

Parameter

Parameter Description
value The value to convert.

Exception

Parameter Description
IllegalArgumentExceptionIf<code>value &lt; Byte#MIN_VALUE</code> or<code>value &gt; #UNSIGNED_MAX</code>.

Return

A byte value equal to (byte)value .

Declaration

static byte toByteValue(final int value) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /** Maximum value of an unsigned 8-bit integer. */
    static final int UNSIGNED_MAX = 0xFF;

    /**/*from w  ww .ja va  2s . c o  m*/
     * Converts a signed or unsigned 8-bit integer into a byte.
     * @param value  The value to convert.
     * @return  A byte value equal to {@code (byte)value}.
     * @throws IllegalArgumentException  If
     *   <code>value &lt; {@link Byte#MIN_VALUE}</code> or
     *   <code>value &gt; {@link #UNSIGNED_MAX}</code>.
     */
    static byte toByteValue(final int value) throws IllegalArgumentException {
        checkByteValue(value);
        return (byte) value;
    }

    /**
     * Checks the given value as a signed or unsigned 8-bit integer.
     * @param value  The value to check.
     * @throws IllegalArgumentException  If
     *   <code>value &lt; {@link Byte#MIN_VALUE}</code> or
     *   <code>value &gt; {@link #UNSIGNED_MAX}</code>.
     */
    static void checkByteValue(int value) throws IllegalArgumentException {
        if (value < Byte.MIN_VALUE) {
            throw new IllegalArgumentException(String.format("value (%d) < minimum (%d)", value, Byte.MIN_VALUE));
        } else if (value > UNSIGNED_MAX) {
            throw new IllegalArgumentException(String.format("value (%d) > maximum (%d)", value, UNSIGNED_MAX));
        }
    }
}

Related

  1. toByteColor(int i)
  2. toByteFromBin(String binSymbols)
  3. toByteMatrix(Number[][] matrix)
  4. toByteObject(String value, Byte defaultValue)
  5. toByteUnit(long values)
  6. toByteWithoutOverflow(float value)