Example usage for java.lang Integer valueOf

List of usage examples for java.lang Integer valueOf

Introduction

In this page you can find the example usage for java.lang Integer valueOf.

Prototype

public static Integer valueOf(String s, int radix) throws NumberFormatException 

Source Link

Document

Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Usage

From source file:Main.java

public static byte[] getByte(String ss) {
    String[] shil = ss.split(" ");
    StringBuilder er = new StringBuilder();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (String string : shil) {
        if ("".equals(string)) {
            continue;
        }/*from ww w  . j ava2  s.c o  m*/
        int i = Integer.valueOf(string, 16);
        out.write(i);
    }
    return out.toByteArray();
}

From source file:Main.java

/**
 * Decodes HEX representation of the string.
 *
 * @param data HEX//from   w  ww.ja va2  s  .  c o m
 * @return String representation of the {@code data}
 * @throws IOException if some error occurs
 */
private static String decodeHex(String data) throws IOException {
    if (data == null || data.isEmpty()) {
        return data;
    }
    if (data.length() % 2 != 0) {
        throw new IOException("String is not in hexadecimal representation.");
    }
    byte[] bytes = new byte[data.length() / 2];
    for (int i = 0; i < data.length(); i += 2) {
        bytes[i / 2] = Integer.valueOf(data.substring(i, i + 2), 16).byteValue();
    }
    return new String(bytes, "UTF-8");
}

From source file:Main.java

public static String decodeUnicode(String str) {
    Charset set = Charset.forName("UTF-16");
    Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
    Matcher m = p.matcher(str);//from www  .  j ava  2 s .  c  o  m
    int start = 0;
    int start2;
    StringBuffer sb = new StringBuffer();
    while (m.find(start)) {
        start2 = m.start();
        if (start2 > start) {
            String seg = str.substring(start, start2);
            sb.append(seg);
        }
        String code = m.group(1);
        int i = Integer.valueOf(code, 16);
        byte[] bb = new byte[4];
        bb[0] = (byte) ((i >> 8) & 0xFF);
        bb[1] = (byte) (i & 0xFF);
        ByteBuffer b = ByteBuffer.wrap(bb);
        sb.append(String.valueOf(set.decode(b)).trim());
        start = m.end();
    }
    start2 = str.length();
    if (start2 > start) {
        String seg = str.substring(start, start2);
        sb.append(seg);
    }
    return sb.toString();
}

From source file:Main.java

public static int parseInt(String txt, int radix, int def) {
    int ret;/*  w w w .  jav a 2 s .c  o m*/
    try {
        ret = Integer.valueOf(txt, radix);
    } catch (Exception e) {
        ret = def;
    }

    return ret;
}

From source file:Main.java

public static byte[] toArray(UUID uuid) {
    String data = uuid.toString().replace("-", "");

    int length = data.length() / 2;

    byte[] result = new byte[length];

    for (int i = 0; i < length; i++) {
        int value = Integer.valueOf(data.substring(2 * i, 2 * i + 2), 16);
        result[i] = (byte) value;
    }/*from  w w  w .j  av  a2 s  .  co m*/

    return result;
}

From source file:Main.java

/**
 * Convert %XX//from w w w .j  av  a2 s .  co m
 * 
 * @param value
 * @return
 */
public static String formParamDecode(String value) {
    int nCount = 0;
    for (int i = 0; i < value.length(); i++) {
        if (value.charAt(i) == '%') {
            i += 2;
        }
        nCount++;
    }

    byte[] sb = new byte[nCount];

    for (int i = 0, index = 0; i < value.length(); i++) {
        if (value.charAt(i) != '%') {
            sb[index++] = (byte) value.charAt(i);
        } else {
            StringBuilder sChar = new StringBuilder();
            sChar.append(value.charAt(i + 1));
            sChar.append(value.charAt(i + 2));
            sb[index++] = Integer.valueOf(sChar.toString(), 16).byteValue();
            i += 2;
        }
    }
    String decode = "";
    try {
        decode = new String(sb, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return decode;
}

From source file:Main.java

public static byte[] convertHexStringToByteArray(String hexString) {
    if ((hexString.length() % 2) != 0) {
        throw new IllegalArgumentException("Invalid hex string (length % 2 != 0)");
    }/*from w  w w.  ja  v a2  s .  co m*/

    byte[] array = new byte[hexString.length() / 2];
    for (int i = 0, arrayIndex = 0; i < hexString.length(); i += 2, arrayIndex++) {
        array[arrayIndex] = Integer.valueOf(hexString.substring(i, i + 2), 16).byteValue();
    }

    return array;
}

From source file:Main.java

public static String UnicodeToString(String Hex) {
    String enUnicode = null;//from w  ww. j  av a  2  s . c o  m
    String deUnicode = null;

    for (int i = 0; i < Hex.length(); i++) {
        if (enUnicode == null)
            enUnicode = String.valueOf(Hex.charAt(i));
        else
            enUnicode = enUnicode + Hex.charAt(i);

        if (i % 4 == 3) {
            if (enUnicode != null) {
                if (deUnicode == null)
                    deUnicode = String.valueOf((char) Integer.valueOf(enUnicode, 16).intValue());
                else
                    deUnicode = deUnicode + String.valueOf((char) Integer.valueOf(enUnicode, 16).intValue());
            }

            enUnicode = null;
        }
    }

    return deUnicode;
}

From source file:Main.java

private static byte[] toByte(String hexString) {
    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;
}

From source file:Main.java

/**
 * Returns a integer representation of the days that the alarm should be
 * recurring on.//www  .  ja va  2 s .  c  om
 * 
 * @param daysOfWeek
 *            The boolean array that represents the days that the alarms
 *            should be recurring on
 * @return The days of the week that should be recurring as bits in a
 *         integer
 */
public static int getIntegerFromBooleanArray(boolean[] daysOfWeek) {
    StringBuilder days = new StringBuilder();
    for (int i = daysOfWeek.length - 1; i >= 0; i--) {
        if (daysOfWeek[i]) {
            days.append("1");
        } else {
            days.append("0");
        }
    }
    return Integer.valueOf(days.toString(), 2);
}