Example usage for java.lang Byte parseByte

List of usage examples for java.lang Byte parseByte

Introduction

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

Prototype

public static byte parseByte(String s, int radix) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed byte in the radix specified by the second argument.

Usage

From source file:Main.java

public static void main(String[] args) {

    System.out.println("parse string to byte:" + Byte.parseByte("10", 8));
}

From source file:Main.java

public static byte parseByteHex(String data) {
    byte value = Byte.parseByte(data, 16);
    return value;
}

From source file:Main.java

public static byte convertToByte(int input) {
    byte out = 0x00;
    String str16 = Integer.toHexString(input);
    out = Byte.parseByte(str16, 16);
    return out;//w w w .ja  v  a2s . c  o m
}

From source file:Main.java

public static byte stringToHexBytes(int obj) {
    byte hex = 0;
    String aim = null;/*from   ww w . ja  v  a 2 s .  c om*/
    aim = String.valueOf(obj);
    if (aim != null && !aim.trim().equals(" ")) {
        hex = Byte.parseByte(aim, 16);
    }
    return hex;
}

From source file:Main.java

public static byte[] hexToBytes(String hex) {
    hex = removeSpaces(hex); // Remove spaces
    assert hex.length() % 2 == 0 : "must be even number of bytes";
    int resultLen = hex.length() / 2;
    byte[] result = new byte[resultLen];
    int j = 0;/*  w  w  w. j  a va 2 s.c o m*/
    for (int i = 0; i < resultLen; i++) {
        result[i] = (byte) (Byte.parseByte(hex.substring(j, ++j), 16) << 4
                | Byte.parseByte(hex.substring(j, ++j), 16));
    }
    return result;
}

From source file:opendap.wcsGateway.UrlEncoder.java

public static String hexToString(String s) {
    String ds = "";
    String achar = "";
    byte b = 0;/*from w ww  .ja va 2  s.  c  o m*/
    int i = 0;

    for (i = 0; i < s.length(); i += 2) {
        if (s.length() >= i + 2)
            achar = s.substring(i, i + 2);
        else
            achar = s.substring(i, i + 1);
        b = Byte.parseByte(achar, 16);
        ds += String.valueOf((char) b);
    }

    return ds;
}

From source file:it.unipmn.di.dcs.common.conversion.Convert.java

/**
 * Converts a hexadecimal string to a byte array.
 *///w  w w . j  a  v  a  2s.  c om
public static byte[] HexToBytes(String hexStr) {
    if (hexStr == null || hexStr.length() == 0) {
        return null;
    }

    // Creates an array whose size is half of the string length.
    // (note: one byte is 2 hex digit).
    byte[] bytes = new byte[hexStr.length() / 2];
    int curIndex = 0;
    for (int i = 0; i < hexStr.length(); ++i) {
        curIndex = (i / 2);
        String hexString = hexStr.substring(i, 2);
        byte newByte = Byte.parseByte(hexString, 16);
        bytes[curIndex] = newByte;
        i++;
    }
    return bytes;
}

From source file:opendap.gateway.HexAsciiEncoder.java

public static String hexToString(String s) throws NumberFormatException {

    if (s == null)
        return null;

    String ds = "";
    String achar = "";
    byte b = 0;// w  w  w  .  java  2  s.  c o  m
    int i = 0;

    for (i = 0; i < s.length(); i += 2) {
        if (s.length() >= i + 2)
            achar = s.substring(i, i + 2);
        else
            achar = s.substring(i, i + 1);
        b = Byte.parseByte(achar, 16);
        ds += String.valueOf((char) b);
    }

    return ds;
}

From source file:org.mabb.fontverter.woff.WoffOutputStream.java

public void writeFlagByte(int flag, int transform) throws IOException {
    String binary = Integer.toBinaryString(flag);
    String transBinary = Integer.toBinaryString(transform);
    if (transBinary.length() < 2)
        transBinary = StringUtils.repeat("0", 2 - transBinary.length()) + transBinary;
    if (binary.length() < 6)
        binary = StringUtils.repeat("0", 6 - binary.length()) + binary;

    binary = transBinary + binary;/*from w ww.  ja va  2  s . c  o m*/
    byte byteOn = (Byte.parseByte(binary, 2));
    write(byteOn);
}

From source file:opendap.gateway.HexAsciiEncoder.java

public static void hexToString(InputStream is, OutputStream os) throws Exception {
    boolean done = false;
    byte[] b = new byte[2];
    int ret;/*from  w ww.  java 2 s .c  om*/
    String achar;
    byte b1;

    while (!done) {
        ret = is.read(b);

        if (ret < 0) {
            done = true;
        } else if (ret != 0) {

            achar = new String(b);
            //System.err.println("achar: "+achar);
            //System.err.println("ret: "+ret);

            b1 = Byte.parseByte(achar, 16);
            os.write(String.valueOf((char) b1).getBytes());
        }
    }

}