Example usage for java.lang Byte decode

List of usage examples for java.lang Byte decode

Introduction

In this page you can find the example usage for java.lang Byte decode.

Prototype

public static Byte decode(String nm) throws NumberFormatException 

Source Link

Document

Decodes a String into a Byte .

Usage

From source file:Main.java

public static void main(String[] args) {

    System.out.println("Decimal 10:" + Byte.decode("10"));
    System.out.println("Octal 10:" + Byte.decode("010"));
    System.out.println("Hex F:" + Byte.decode("0XF"));

    System.out.println("Negative Hex F:" + Byte.decode("-0XF"));
}

From source file:Main.java

public static byte uniteByte(byte src0, byte src1) {
    byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
    _b0 = (byte) (_b0 << 4);
    byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
    byte ret = (byte) (_b0 ^ _b1);
    return ret;//from w  w w.j  av  a 2  s . c o  m
}

From source file:Main.java

private static byte uniteBytes(byte src0, byte src1) {
    byte _b0 = Byte.decode(new String(new byte[] { src0 })).byteValue();
    _b0 = (byte) (_b0 << 4);
    byte _b1 = Byte.decode(new String(new byte[] { src1 })).byteValue();
    byte ret = (byte) (_b0 | _b1);
    byte aret = Byte.decode("0x" + ret).byteValue();

    return aret;//  ww  w .j  a  v a  2s.c  o m
}

From source file:Main.java

public static byte[] hexStr2Bytes(String src) {
    int m = 0, n = 0;
    int l = src.length() / 2;
    byte[] ret = new byte[l];
    for (int i = 0; i < l; i++) {
        m = i * 2 + 1;/* w  w w  .j  a v a 2  s.c  o  m*/
        n = m + 1;
        ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
    }
    return ret;
}

From source file:Main.java

public static byte uniteBytes(byte src0, byte src1) {
    byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
    _b0 = (byte) (_b0 << 4);
    byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
    byte ret = (byte) (_b0 ^ _b1);
    return ret;//w w  w.j  av a 2  s. c o m
}

From source file:Main.java

public static byte uniteBytes(byte src0, byte src1) {
    byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
    _b0 = (byte) (_b0 << 4);
    byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
    byte ret = (byte) (_b0 | _b1);
    return ret;/*from w w  w  .  j  a  v  a 2  s .  com*/
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 * @param text the text to convert//from   w  w w.j a va2  s . co m
 * @param targetClass the target class to parse into
 * @return the parsed number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class<?> targetClass) {
    //   Assert.notNull(text, "Text must not be null");
    //Assert.notNull(targetClass, "Target class must not be null");

    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 *
 * @param text        the text to convert
 * @param targetClass the target class to parse into
 * @return the parsed number//www.j av  a2  s .com
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class targetClass) {
    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:NumberUtils.java

/**
 * Parse the given text into a number instance of the given target class,
 * using the corresponding default <code>decode</code> methods. Trims the
 * input <code>String</code> before attempting to parse the number. Supports
 * numbers in hex format (with leading 0x) and in octal format (with leading 0).
 * @param text the text to convert//from   w  ww .jav a  2 s .c  o  m
 * @param targetClass the target class to parse into
 * @return the parsed number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte#decode
 * @see java.lang.Short#decode
 * @see java.lang.Integer#decode
 * @see java.lang.Long#decode
 * @see #decodeBigInteger(String)
 * @see java.lang.Float#valueOf
 * @see java.lang.Double#valueOf
 * @see java.math.BigDecimal#BigDecimal(String)
 */
public static Number parseNumber(String text, Class targetClass) {

    String trimmed = text.trim();

    if (targetClass.equals(Byte.class)) {
        return Byte.decode(trimmed);
    } else if (targetClass.equals(Short.class)) {
        return Short.decode(trimmed);
    } else if (targetClass.equals(Integer.class)) {
        return Integer.decode(trimmed);
    } else if (targetClass.equals(Long.class)) {
        return Long.decode(trimmed);
    } else if (targetClass.equals(BigInteger.class)) {
        return decodeBigInteger(trimmed);
    } else if (targetClass.equals(Float.class)) {
        return Float.valueOf(trimmed);
    } else if (targetClass.equals(Double.class)) {
        return Double.valueOf(trimmed);
    } else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
        return new BigDecimal(trimmed);
    } else {
        throw new IllegalArgumentException(
                "Cannot convert String [" + text + "] to target class [" + targetClass.getName() + "]");
    }
}

From source file:com.l2jfree.gameserver.templates.StatsSet.java

/**
 * Returns the int associated to the key put in parameter ("name"). If the value associated to the key is null, this method returns the value of the parameter deflt.
 * //from ww w.j  a v a 2s. co m
 * @param name : String designating the key in the set
 * @param deflt : byte designating the default value if value associated with the key is null
 * @return byte : value associated to the key
 */
public final byte getByte(String name, byte deflt) {
    Object val = get(name);
    if (val == null)
        return deflt;
    if (val instanceof Number)
        return ((Number) val).byteValue();
    try {
        return Byte.decode((String) val);
    } catch (Exception e) {
        throw new IllegalArgumentException("Byte value required, but found: " + val);
    }
}