Example usage for java.lang Integer toHexString

List of usage examples for java.lang Integer toHexString

Introduction

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

Prototype

public static String toHexString(int i) 

Source Link

Document

Returns a string representation of the integer argument as an unsigned integer in base 16.

Usage

From source file:Main.java

/**
 * <p>Converts the string to the Unicode format '\u0020'.</p>
 * //from   w ww.  j  a v  a  2  s .co  m
 * <p>This format is the Java source code format.</p>
 *
 * <pre>
 *   CharUtils.unicodeEscaped(' ') = "\u0020"
 *   CharUtils.unicodeEscaped('A') = "\u0041"
 * </pre>
 * 
 * @param ch  the character to convert
 * @return the escaped Unicode string
 */
public static String unicodeEscaped(final char ch) {
    if (ch < 0x10) {
        return "\\u000" + Integer.toHexString(ch);
    } else if (ch < 0x100) {
        return "\\u00" + Integer.toHexString(ch);
    } else if (ch < 0x1000) {
        return "\\u0" + Integer.toHexString(ch);
    }
    return "\\u" + Integer.toHexString(ch);
}

From source file:Main.java

public static int firstDiff(byte[] a, int aOffset, byte[] b, int bOffset, int length) {
    if (a == null || b == null) {
        throw new IllegalArgumentException("Cannot compare null arrays.");
    }/*from w w  w.j a  va2s .  c  om*/
    if (aOffset + length > a.length || bOffset + length > b.length) {
        throw new ArrayIndexOutOfBoundsException("Range to compare not contained in array.");
    }
    if (a == b && aOffset == bOffset) {
        return -1;
    }
    for (int i = 0; i < length; i++) {
        if (a[aOffset + i] != b[bOffset + i]) {
            System.out.println(i + ": " + Integer.toHexString(0xFF & a[aOffset + i]) + " "
                    + Integer.toHexString(0xFF & b[bOffset + i]));
            return i;
        }
    }
    return -1;
}

From source file:com.beginner.core.utils.MD5.java

public static String md5(String str) {
    try {/* ww  w  .j  av a  2 s. co m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte b[] = md.digest();

        int i;

        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        str = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();

    }
    return str;
}

From source file:Main.java

public static float getAttributeDimension(final Context context, final Resources.Theme theme, final int resId) {
    final TypedValue typedValue = new TypedValue(); // create a new typed value to received the resolved attribute
    // value//from w w  w .  j ava  2  s  .  c o  m
    final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    if (!theme.resolveAttribute(resId, typedValue, true)) // if we can't resolve the value
    {
        throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(resId));
    }
    if (typedValue.type != TypedValue.TYPE_DIMENSION) // if the value isn't of the correct type
    {
        throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(resId) + " type #0x"
                + Integer.toHexString(typedValue.type) + " is not valid");
    }
    return typedValue.getDimension(displayMetrics); // return the value of the attribute in terms of the display
}

From source file:com.microsoft.tfs.client.common.ui.framework.helper.ColorUtils.java

/**
 * Returns a hexadecimal string ("html color") describing the given color.
 *
 * @param color/*from w w w  .  j  a v  a2s  .co m*/
 *        The color to describe in hexadecimal notation (not null)
 * @return A hexadecimal string (including leading "#") that describes the
 *         color
 */
public static String getColorString(final Color color) {
    Check.notNull(color, "color"); //$NON-NLS-1$

    final String red = Integer.toHexString(color.getRed());
    final String green = Integer.toHexString(color.getGreen());
    final String blue = Integer.toHexString(color.getBlue());

    final StringBuffer sb = new StringBuffer("#"); //$NON-NLS-1$

    if (red.length() == 1) {
        sb.append("0"); //$NON-NLS-1$
    }
    sb.append(red);

    if (green.length() == 1) {
        sb.append("0"); //$NON-NLS-1$
    }
    sb.append(green);

    if (blue.length() == 1) {
        sb.append("0"); //$NON-NLS-1$
    }
    sb.append(blue);

    return sb.toString();
}

From source file:Main.java

/**
 * Convert capitalize mode flags into human readable text.
 *
 * @param capsFlags The modes flags to be converted. It may be any combination of
 * {@link TextUtils#CAP_MODE_CHARACTERS}, {@link TextUtils#CAP_MODE_WORDS}, and
 * {@link TextUtils#CAP_MODE_SENTENCES}.
 * @return the text that describe the <code>capsMode</code>.
 *//*from  www  .j av  a  2 s . co  m*/
public static String flagsToString(final int capsFlags) {
    final int capsFlagsMask = TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS
            | TextUtils.CAP_MODE_SENTENCES;
    if ((capsFlags & ~capsFlagsMask) != 0) {
        return "unknown<0x" + Integer.toHexString(capsFlags) + ">";
    }
    final ArrayList<String> builder = new ArrayList<>();
    if ((capsFlags & android.text.TextUtils.CAP_MODE_CHARACTERS) != 0) {
        builder.add("characters");
    }
    if ((capsFlags & android.text.TextUtils.CAP_MODE_WORDS) != 0) {
        builder.add("words");
    }
    if ((capsFlags & android.text.TextUtils.CAP_MODE_SENTENCES) != 0) {
        builder.add("sentences");
    }
    return builder.isEmpty() ? "none" : TextUtils.join("|", builder);
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash = null;
    try {// w ww.  jav a  2 s. co m
        hash = MessageDigest.getInstance("MD5").digest( //No i18n
                string.getBytes("UTF-8"));//No i18n
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10) {
            hex.append("0");//No i18n
        }
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

/**
 * Escapes the given text such that it can be safely embedded in a string literal
 * in Java source code./*from   www .  jav a  2  s .  com*/
 * 
 * @param text the text to escape
 * 
 * @return the escaped text
 */
public static String escapeToJavaString(String text) {
    if (text == null) {
        return null;
    }
    String result = text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replace("\b", "\\b")
            .replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
    StringBuilder complete = new StringBuilder();
    for (int i = 0; i < result.length(); i++) {
        int codePointI = result.codePointAt(i);
        if (codePointI >= 32 && codePointI <= 127) {
            complete.append(Character.toChars(codePointI));
        } else {
            // use Unicode representation
            complete.append("\\u");
            String hex = Integer.toHexString(codePointI);
            complete.append(getRepeatingString(4 - hex.length(), '0'));
            complete.append(hex);
        }
    }
    return complete.toString();
}

From source file:com.github.jinahya.codec.HexEncoderTest.java

@Test(enabled = true)
public static void testEncodeSingle() {

    final byte[] output = new byte[2];
    for (int i = 0; i < 256; i++) {
        HexEncoder.encodeSingle(i, output, 0);
        final String hex = (i < 0x10 ? "0" : "") + Integer.toHexString(i).toUpperCase();
        Assert.assertEquals(new String(output, StandardCharsets.US_ASCII), hex);
    }//from   ww  w. jav  a 2 s  . c  o m
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

public static String md5L32(String unencryptedText) {
    if (StringUtils.isEmpty(unencryptedText)) {
        LOGGER.info("unencryptedText is null.");
        return unencryptedText;
    }/*from  w w w . j a va2s  .c o m*/

    String ciphertext = "";
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(unencryptedText.getBytes("UTF-8"));
        StringBuffer buffer = new StringBuffer();
        for (byte b : bytes) {
            int bt = b & 0xff;
            if (bt < 16) {
                buffer.append(0);
            }
            buffer.append(Integer.toHexString(bt));
        }
        ciphertext = buffer.toString();
        LOGGER.info("encrypt string {} to {};", unencryptedText, ciphertext);
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    return ciphertext;
}