Example usage for java.lang Character forDigit

List of usage examples for java.lang Character forDigit

Introduction

In this page you can find the example usage for java.lang Character forDigit.

Prototype

public static char forDigit(int digit, int radix) 

Source Link

Document

Determines the character representation for a specific digit in the specified radix.

Usage

From source file:com.jaspersoft.jasperserver.core.util.StringUtil.java

/**
 * Convert byteArr to hex sting./*from  w  w  w. ja  v  a2 s. c  o m*/
 * @param byteArr
 * @return
 */
public static String byteArrayToHexString(byte[] byteArr) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteArr.length; i++) {
        byte b = byteArr[i];
        int high = (b & 0xF0) >> 4;
        int low = b & 0x0F;

        sb.append(Character.forDigit(high, 16));
        sb.append(Character.forDigit(low, 16));
    }
    return sb.toString();
}

From source file:org.thelq.stackexchange.api.StackClient.java

protected URI createUri(@NonNull BaseQuery<?, ?> query) {
    //Run query verification
    Map<String, String> finalParameters = query.buildFinalParameters();
    if (query.isAuthRequired() && StringUtils.isBlank(accessToken))
        throw new RuntimeException("Query " + query.getClass().getName() + " requires an accessToken");
    String method = query.getMethod().getFinal();
    if (method.contains("{}"))
        throw new RuntimeException("Unreplaced vector remaining in method " + method);

    //Build a URI manually
    StringBuilder uriBuilder = new StringBuilder("https://api.stackexchange.com/2.1/").append(method)
            .append("?");
    if (StringUtils.isNotBlank(seApiKey))
        uriBuilder.append("key=").append(seApiKey).append("&");
    for (Map.Entry<String, String> curParam : finalParameters.entrySet()) {
        if (curParam.getKey() == null || curParam.getValue() == null)
            throw new NullPointerException(
                    "Parameters cannot be null: " + curParam.getKey() + "=" + curParam.getValue());
        uriBuilder.append(curParam.getKey()).append("=");

        //Encode value
        final ByteBuffer bb = Charsets.UTF_8.encode(curParam.getValue());
        while (bb.hasRemaining()) {
            final int b = bb.get() & 0xff;
            if (URI_SAFECHARS.get(b))
                uriBuilder.append((char) b);
            else if (b == ' ')
                uriBuilder.append('+');
            else {
                uriBuilder.append("%");
                final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
                final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
                uriBuilder.append(hex1);
                uriBuilder.append(hex2);
            }//from ww w .  j  ava  2 s  .c om
        }
        uriBuilder.append("&");
    }
    char lastChar = uriBuilder.charAt(uriBuilder.length() - 1);
    if (lastChar == '&' || lastChar == '?')
        uriBuilder.deleteCharAt(uriBuilder.length() - 1);

    return URI.create(uriBuilder.toString());
}

From source file:org.light.portal.util.StringUtils.java

public static String encode(String s) {
    int maxBytesPerChar = 10;
    StringBuffer out = new StringBuffer(s.length());
    java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream(maxBytesPerChar);
    java.io.OutputStreamWriter writer = new java.io.OutputStreamWriter(buf);

    for (int i = 0; i < s.length(); i++) {
        int c = (int) s.charAt(i);
        if (dontNeedEncoding.get(c)) {
            out.append((char) c);
        } else {//  w w  w.  j a  v  a2  s  .  c  o m
            // convert to external encoding before hex conversion
            try {
                writer.write(c);
                writer.flush();
            } catch (java.io.IOException e) {
                buf.reset();
                continue;
            }
            byte[] ba = buf.toByteArray();
            for (int j = 0; j < ba.length; j++) {
                out.append('x');
                char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
                // converting to use uppercase letter as part of
                // the hex value if ch is a letter.
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
                ch = Character.forDigit(ba[j] & 0xF, 16);
                if (Character.isLetter(ch)) {
                    ch -= caseDiff;
                }
                out.append(ch);
            }
            buf.reset();
        }
    }

    return out.toString();
}

From source file:com.criticalsoftware.mobics.presentation.action.account.EditInformationContactActionBean.java

/**
 * Validates the NIF// w  w w . j a v a  2 s  .  c  o m
 * 
 * @param nif
 * @return true if valid
 */
private boolean isValidNIF(final String nif) {
    // Verifica se  nulo, se  numrico e se tem 9 dgitos
    if ((nif != null) && StringUtils.isNumeric(nif) && (nif.length() == 9)) {
        // Obtem o primeiro nmero do NIF
        char c = nif.charAt(0);
        // Verifica se o primeiro nmero  (1, 2, 5, 6, 8 ou 9)
        if ((c == '1') || (c == '2') || (c == '5') || (c == '6') || (c == '8') || (c == '9')) {
            // Calculo do Digito de Controle
            int checkDigit = c * 9;
            for (int i = 2; i <= 8; i++) {
                checkDigit += nif.charAt(i - 1) * (10 - i);
            }
            checkDigit = 11 - (checkDigit % 11);

            // Se o digito de controle  maior que dez, coloca-o a zero
            if (checkDigit >= 10) {
                checkDigit = 0;
            }

            // Compara o digito de controle com o ltimo numero do NIF
            // Se igual, o NIF  vlido.
            if (Character.forDigit(checkDigit, 10) == nif.charAt(8)) {
                return true;
            }
        }
    }
    return false;
}

From source file:net.ymate.framework.core.support.TokenProcessHelper.java

/**
 * Convert a byte array to a String of hexadecimal digits and return it.
 *
 * @param buffer The byte array to be converted
 *///from  w  w w.jav  a2 s  .c  om
private String toHex(byte[] buffer) {
    StringBuilder sb = new StringBuilder(buffer.length * 2);
    for (byte aBuffer : buffer) {
        sb.append(Character.forDigit((aBuffer & 0xf0) >> 4, 16));
        sb.append(Character.forDigit(aBuffer & 0x0f, 16));
    }
    return sb.toString();
}

From source file:com.nfc.gemkey.MainActivity.java

private String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder("");
    if (src == null || src.length <= 0) {
        return null;
    }/*from  ww w  . ja  v a2s.  c o  m*/

    char[] buffer = new char[2];
    for (int i = 0; i < src.length; i++) {
        buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
        buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
        System.out.println(buffer);
        stringBuilder.append(buffer);
    }

    return stringBuilder.toString();
}

From source file:org.jenkinsci.remoting.engine.HandlerLoopbackLoadStress.java

private static String secretFor(@Nonnull String clientName) {
    try {//from ww  w  .  j  a v  a2 s.  c o m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.reset();
        byte[] bytes = digest
                .digest((HandlerLoopbackLoadStress.class.getName() + clientName).getBytes(Charsets.UTF_8));
        StringBuilder result = new StringBuilder(Math.max(0, bytes.length * 3 - 1));
        for (int i = 0; i < bytes.length; i++) {
            if (i > 0) {
                result.append(':');
            }
            result.append(Character.forDigit((bytes[i] >> 4) & 0x0f, 16));
            result.append(Character.forDigit(bytes[i] & 0x0f, 16));
        }
        return result.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("JLS mandates MD5 support");
    }
}

From source file:acp.sdk.SecureUtil.java

/**
 * byte???// w ww . ja  va2 s  . c o m
 * 
 * @param b
 *            ?byte
 * @return ??
 */
public static String Hex2Str(byte[] b) {
    StringBuffer d = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++) {
        char hi = Character.forDigit((b[i] >> 4) & 0x0F, 16);
        char lo = Character.forDigit(b[i] & 0x0F, 16);
        d.append(Character.toUpperCase(hi));
        d.append(Character.toUpperCase(lo));
    }
    return d.toString();
}

From source file:com.box.restclientv2.httpclientsupport.HttpClientURLEncodedUtils.java

/**
 * Emcode/escape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 * //from  ww w. j  a v  a 2  s  . c o m
 * @param content
 *            the portion to decode
 * @param charset
 *            the charset to use
 * @param blankAsPlus
 *            if {@code true}, then convert space to '+' (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return
 */
private static String urlencode(final String content, final Charset charset, final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}

From source file:org.springframework.social.oauth1.SigningSupport.java

private static byte[] encode(byte[] source, BitSet notEncoded) {
    Assert.notNull(source, "'source' must not be null");
    ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length * 2);
    for (int i = 0; i < source.length; i++) {
        int b = source[i];
        if (b < 0) {
            b += 256;/* ww w  .j  av  a  2  s  .  co  m*/
        }
        if (notEncoded.get(b)) {
            bos.write(b);
        } else {
            bos.write('%');
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
            bos.write(hex1);
            bos.write(hex2);
        }
    }
    return bos.toByteArray();
}