Java Utililty Methods Byte Create

List of utility methods to do Byte Create

Description

The list of methods to do Byte Create are organized into topic(s).

Method

ObjecttoByte(Object value)
to Byte
if (value instanceof Number) {
    return ((Number) value).byteValue();
if (value instanceof String && !((String) value).isEmpty()) {
    return Byte.valueOf((String) value);
return null;
BytetoByte(Object value)
Convert an Object to a Byte.
if (value == null)
    return null;
if (value instanceof Byte)
    return (Byte) value;
if (value instanceof String) {
    if ("".equals((String) value))
        return null;
    return new Byte((String) value);
...
bytetoByte(short unsignedByte)
Converts a short containing an unsigned byte value to the corresponding signed byte value and returns the result as a byte
if (unsignedByte < 0 || unsignedByte > MAX_BYTE) {
    throw new IllegalArgumentException(String.format(
            "Unsigned byte values should be in the range 0..%1$d, got: %2$d", MAX_BYTE, unsignedByte));
return (byte) unsignedByte;
bytetoByte(short value, boolean first)
to Byte
if (first)
    return (byte) (0xff & (value >> 8));
return (byte) (0xff & value);
bytetoByte(String hex)
to Byte
return (byte) Integer.parseInt(hex, 16);
bytetoByte(String hex)
to Byte
char[] lowChar = { '#', '0' };
char[] highChar = { '#', '0' };
int start = 0;
if (hex.startsWith("#"))
    start = 1;
else if (hex.startsWith("0x"))
    start = 2;
if (hex.length() > start + 1) {
...
bytetoByte(String hex)
returns byte of given hex string expression.
if (hex.startsWith(HEX_PREFIX)) {
    hex = hex.replace(HEX_PREFIX, "");
int i = Integer.parseInt(hex, 16);
return (byte) i;
byte[]toByte(String hexStr)
to Byte
byte digest[] = new byte[hexStr.length() / 2];
for (int i = 0; i < digest.length; i++) {
    String byteString = hexStr.substring(2 * i, 2 * i + 2);
    int byteValue = Integer.parseInt(byteString, 16);
    digest[i] = (byte) byteValue;
return digest;
byte[]toByte(String hexString)
to Byte
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
    result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
return result;
bytetoByte(String str)

Convert a String to a byte, returning zero if the conversion fails.

If the string is null, zero is returned.

 NumberUtils.toByte(null) = 0 NumberUtils.toByte("")   = 0 NumberUtils.toByte("1")  = 1 
return toByte(str, (byte) 0);