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:org.apache.jasper.compiler.JspUtil.java

/**
 * Mangle the specified character to create a legal Java class name.
 */// w ww  . j a v a2 s  .  c  o m
public static final String mangleChar(char ch) {
    char[] result = new char[5];
    result[0] = '_';
    result[1] = Character.forDigit((ch >> 12) & 0xf, 16);
    result[2] = Character.forDigit((ch >> 8) & 0xf, 16);
    result[3] = Character.forDigit((ch >> 4) & 0xf, 16);
    result[4] = Character.forDigit(ch & 0xf, 16);
    return new String(result);
}

From source file:com.orange.api.atmosdav.AtmosDavServlet.java

public static String encode(String s) {
    int maxBytesPerChar = 10; // rather arbitrary limit, but safe for now
    StringBuffer out = new StringBuffer(s.length());
    ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);

    try {/*from ww w .  j  a v  a 2 s  .c  o  m*/
        OutputStreamWriter writer = new OutputStreamWriter(buf, dfltEncName);

        for (int i = 0; i < s.length(); i++) {
            int c = (int) s.charAt(i);

            if (dontNeedEncoding.get(c)) {
                out.append((char) c);
            } else {
                // convert to external encoding before hex conversion
                try {
                    writer.write(c);
                    /*
                     * If this character represents the start of a Unicode
                     * surrogate pair, then pass in two characters. It's not
                     * clear what should be done if a bytes reserved in the
                     * surrogate pairs range occurs outside of a legal
                     * surrogate pair. For now, just treat it as if it were
                     * any other character.
                     */
                    if (c >= 0xD800 && c <= 0xDBFF) {
                        if ((i + 1) < s.length()) {
                            int d = (int) s.charAt(i + 1);
                            if (d >= 0xDC00 && d <= 0xDFFF) {
                                writer.write(d);
                                i++;
                            }
                        }
                    }
                    writer.flush();
                } catch (IOException e) {
                    buf.reset();
                    continue;
                }
                byte[] ba = buf.toByteArray();
                for (int j = 0; j < ba.length; j++) {
                    out.append('%');
                    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();
    } catch (UnsupportedEncodingException e) {
        return s;
    }

}

From source file:org.latticesoft.util.common.MiscUtil.java

/**
 * Generates a random alpha numeric string according to the format.
 * X: upper case letters//ww w. jav  a 2 s  .  c  om
 * x: lower case letters
 * n: numeric numbers
 * @param format the format of the random string
 * @param maxSleep the randomness seed of the sleep
 */
public static String getRandomString(String format, int maxSleep) {
    char c = ' ';
    if (format == null || format.length() == 0) {
        return "";
    }
    StringBuffer sb = new StringBuffer();
    int theChar = 0;
    String s = null;
    for (int i = 0; i < format.length(); i++) {
        c = format.charAt(i);
        s = " ";
        if (c == 'X' || c == 'A') {
            theChar = 10 + (NumeralUtil.getRandomInt(26 * 5000) % 26);
            c = Character.forDigit(theChar, Character.MAX_RADIX);
            s = "" + Character.toUpperCase(c);
            sb.append(s);
        } else if (c == 'x' || c == 'a') {
            theChar = 10 + (NumeralUtil.getRandomInt(26 * 5000) % 26);
            c = Character.forDigit(theChar, Character.MAX_RADIX);
            s = "" + Character.toLowerCase(c);
            sb.append(s);
        } else if (c == 'n' || c == 'N' || c == '0') {
            s = "" + (NumeralUtil.getRandomInt(5000) % 10);
            sb.append(s);
        } else if (c == 'y' || c == 'Y') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 'y' || c1 == 'Y') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 2 || size == 4) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else if (c == 'M') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 'M') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 2 || size == 3) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else if (c == 'd') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 'd') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 2) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else if (c == 'H') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 'H') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 2) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else if (c == 'm') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 'm') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 2) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else if (c == 's') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 's') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 2) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else if (c == 'S') {
            char c1 = ' ';
            StringBuffer sb2 = new StringBuffer();
            for (int j = i; j < format.length(); j++) {
                c1 = format.charAt(j);
                if (c1 == 'S') {
                    sb2.append(c1);
                } else {
                    break;
                }
            }
            int size = sb2.length();
            Timestamp ts = DateUtil.getCurrentTimestamp();
            if (size == 3) {
                sb.append(DateUtil.formatDate(sb2.toString(), ts));
            } else {
                sb.append(sb2.toString());
            }
            i += size - 1;
        } else {
            s = "" + c;
            sb.append(s);
        }
        ThreadUtil.randomSleep(maxSleep);
    }
    return sb.toString();
}

From source file:edu.oregonstate.eecs.mcplan.util.Fn.java

/**
 * Convert a BitSet to a string in which each character represents
 * 'block_size' consecutive bits./*from  w w  w  . j  a va 2 s .co m*/
 * @param bits
 * @param block_size Min 1, max 5
 * @return
 */
public static String toDigits(final BitSet bits, final int block_size) {
    assert (block_size >= 1);
    assert (block_size <= 5);
    final int radix = 1 << block_size;
    final StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bits.length(); i += block_size) {
        int c = 0;
        for (int j = 0; j < block_size; ++j) {
            c |= (bits.get(i + j) ? 1 : 0) << j;
        }
        sb.append(Character.forDigit(c, radix));
    }
    return sb.reverse().toString();
}

From source file:org.apache.wookie.beans.jcr.JCRPersistenceManager.java

/**
 * Escape invalid JCR characters in name.
 * //from  w  w  w . j  ava 2  s  . c om
 * @param name original name
 * @return escaped or original name
 */
public static String escapeJCRName(String name) {
    StringBuilder escapedName = null;
    if (name != null) {
        for (int i = 0, limit = name.length(); (i < limit); i++) {
            char nameChar = name.charAt(i);
            switch (nameChar) {
            case '%':
            case '/':
            case ':':
            case '[':
            case ']':
            case '|':
            case '*':
                if (escapedName == null) {
                    escapedName = new StringBuilder(name.substring(0, i));
                }
                escapedName.append('%');
                escapedName.append(Character.toUpperCase(Character.forDigit(nameChar / 16, 16)));
                escapedName.append(Character.toUpperCase(Character.forDigit(nameChar % 16, 16)));
                break;
            default:
                if (escapedName != null) {
                    escapedName.append(nameChar);
                }
                break;
            }
        }
    }
    return ((escapedName != null) ? escapedName.toString() : name);
}

From source file:com.welfare.common.util.UtilValidate.java

public static char calcChecksum(String value, int length) {
    if (value != null && value.length() == length + 1) {
        value = value.substring(0, length);
    }//from   w  ww  . ja v a 2  s  .  c om
    if (value == null || value.length() != length) {
        throw new IllegalArgumentException(
                "Illegal size of value; must be either" + length + " or " + (length + 1) + " characters");
    }
    int oddsum = 0;
    int evensum = 0;
    for (int i = value.length() - 1; i >= 0; i--) {
        if ((value.length() - i) % 2 == 0) {
            evensum += Character.digit(value.charAt(i), 10);
        } else {
            oddsum += Character.digit(value.charAt(i), 10);
        }
    }
    int check = 10 - ((evensum + 3 * oddsum) % 10);
    if (check >= 10)
        check = 0;
    return Character.forDigit(check, 10);
}

From source file:org.apache.flex.swf.io.SWFDump.java

@SuppressWarnings("unused")
private String hexify(byte[] id) {
    StringBuilder b = new StringBuilder(id.length * 2);
    for (int i = 0; i < id.length; i++) {
        b.append(Character.forDigit((id[i] >> 4) & 15, 16));
        b.append(Character.forDigit(id[i] & 15, 16));
    }//from   www.j  a va2s . c  o  m
    return b.toString().toUpperCase();
}

From source file:ImageAnalyzer.java

String dataHexDump(String lineDelimiter) {
    if (image == null)
        return "";
    char[] dump = new char[imageData.height * (6 + 3 * imageData.bytesPerLine + lineDelimiter.length())];
    int index = 0;
    for (int i = 0; i < imageData.data.length; i++) {
        if (i % imageData.bytesPerLine == 0) {
            int line = i / imageData.bytesPerLine;
            dump[index++] = Character.forDigit(line / 1000 % 10, 10);
            dump[index++] = Character.forDigit(line / 100 % 10, 10);
            dump[index++] = Character.forDigit(line / 10 % 10, 10);
            dump[index++] = Character.forDigit(line % 10, 10);
            dump[index++] = ':';
            dump[index++] = ' ';
        }//w ww.ja  va  2s.  com
        byte b = imageData.data[i];
        dump[index++] = Character.forDigit((b & 0xF0) >> 4, 16);
        dump[index++] = Character.forDigit(b & 0x0F, 16);
        dump[index++] = ' ';
        if ((i + 1) % imageData.bytesPerLine == 0) {
            dump[index++] = lineDelimiter.charAt(0);
            if (lineDelimiter.length() > 1)
                dump[index++] = lineDelimiter.charAt(1);
        }
    }
    String result = "";
    try {
        result = new String(dump);
    } catch (OutOfMemoryError e) {
        /* Too much data to display in the text widget - truncate at 4M. */
        result = new String(dump, 0, 4 * 1024 * 1024) + "\n ...data dump truncated at 4M...";
    }
    return result;
}

From source file:org.eclipse.swt.examples.imageanalyzer.ImageAnalyzer.java

String dataHexDump(String lineDelimiter) {
    final int MAX_DUMP = 1024 * 1024;
    if (image == null)
        return "";
    boolean truncated = false;
    char[] dump = null;
    byte[] alphas = imageData.alphaData;
    try {/* w w  w  . j  a  v  a2  s. c  o m*/
        int length = imageData.height * (6 + 3 * imageData.bytesPerLine + lineDelimiter.length());
        if (alphas != null && alphas.length > 0) {
            length += imageData.height * (6 + 3 * imageData.width + lineDelimiter.length()) + 6
                    + lineDelimiter.length();
        }
        dump = new char[length];
    } catch (OutOfMemoryError e) {
        /* Too much data to dump - truncate. */
        dump = new char[MAX_DUMP];
        truncated = true;
    }
    int index = 0;
    try {
        for (int i = 0; i < imageData.data.length; i++) {
            if (i % imageData.bytesPerLine == 0) {
                int line = i / imageData.bytesPerLine;
                dump[index++] = Character.forDigit(line / 1000 % 10, 10);
                dump[index++] = Character.forDigit(line / 100 % 10, 10);
                dump[index++] = Character.forDigit(line / 10 % 10, 10);
                dump[index++] = Character.forDigit(line % 10, 10);
                dump[index++] = ':';
                dump[index++] = ' ';
            }
            byte b = imageData.data[i];
            dump[index++] = Character.forDigit((b & 0xF0) >> 4, 16);
            dump[index++] = Character.forDigit(b & 0x0F, 16);
            dump[index++] = ' ';
            if ((i + 1) % imageData.bytesPerLine == 0) {
                dump[index++] = lineDelimiter.charAt(0);
                if (lineDelimiter.length() > 1) {
                    dump[index++] = lineDelimiter.charAt(1);
                }
            }
        }
        if (alphas != null && alphas.length > 0) {
            dump[index++] = lineDelimiter.charAt(0);
            if (lineDelimiter.length() > 1) {
                dump[index++] = lineDelimiter.charAt(1);
            }
            System.arraycopy(new char[] { 'A', 'l', 'p', 'h', 'a', ':' }, 0, dump, index, 6);
            index += 6;
            dump[index++] = lineDelimiter.charAt(0);
            if (lineDelimiter.length() > 1) {
                dump[index++] = lineDelimiter.charAt(1);
            }
            for (int i = 0; i < alphas.length; i++) {
                if (i % imageData.width == 0) {
                    int line = i / imageData.width;
                    dump[index++] = Character.forDigit(line / 1000 % 10, 10);
                    dump[index++] = Character.forDigit(line / 100 % 10, 10);
                    dump[index++] = Character.forDigit(line / 10 % 10, 10);
                    dump[index++] = Character.forDigit(line % 10, 10);
                    dump[index++] = ':';
                    dump[index++] = ' ';
                }
                byte b = alphas[i];
                dump[index++] = Character.forDigit((b & 0xF0) >> 4, 16);
                dump[index++] = Character.forDigit(b & 0x0F, 16);
                dump[index++] = ' ';
                if ((i + 1) % imageData.width == 0) {
                    dump[index++] = lineDelimiter.charAt(0);
                    if (lineDelimiter.length() > 1) {
                        dump[index++] = lineDelimiter.charAt(1);
                    }
                }
            }
        }
    } catch (IndexOutOfBoundsException e) {
    }
    String result = "";
    try {
        result = new String(dump);
    } catch (OutOfMemoryError e) {
        /* Too much data to display in the text widget - truncate. */
        result = new String(dump, 0, MAX_DUMP);
        truncated = true;
    }
    if (truncated)
        result += "\n ...data dump truncated at " + MAX_DUMP + "bytes...";
    return result;
}

From source file:de.main.sessioncreator.DesktopApplication1View.java

private void cleanupReviewPanel() {
    for (int i = 0; i < reviewCmbxSessiontoReview.getItemCount(); i++) {
        if (reviewCmbxSessiontoReview.getItemAt(i)
                .equals(reviewCmbxSessiontoReview.getSelectedItem().toString())) {
            reviewCmbxSessiontoReview.removeItemAt(i);
            break;
        }/*from w w w .ja v  a  2s.c om*/
    }
    reviewCmbxSessiontoReview.setSelectedIndex(0);
    Component[] compo = reviewtoReviewPanel.getComponents();
    for (Component c : compo) {
        if (c instanceof JLabel) {
            JLabel jl = (JLabel) c;
            if (jl.getText().contains(reviewtfNameOfTester.getText())) {
                char nc;
                String text = jl.getText();
                nc = text.charAt(jl.getText().lastIndexOf(":") + 2);
                int no = Character.digit(nc, 10);
                no--;
                jl.setText(text.replace(text.charAt(text.lastIndexOf(":") + 2), Character.forDigit(no, 10)));
                reviewtoReviewPanel.revalidate();
                reviewtoReviewPanel.repaint();
                break;
            }
        }
    }
    reviewtaCharterdynamic.setText("");
    reviewtaTestsessionAreasReview.setText("");
    reviewLblStartTime.setText("");
    reviewtfNameOfTester.setText("");
    reviewtfDuration.setText("");
    reviewtfSessionSetup.setText("");
    reviewtfTestDesignExecution.setText("");
    reviewtfBugInvestigation.setText("");
    reviewtfCharter.setText("");
    reviewtaDataFiles.setText("");
    reviewtaTestNotes.setText("");
    reviewedipaneBugs.setText("");
    reviewtaIssues.setText("");
    reviewtaReview.setText("");
}