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:com.gwtcx.server.util.Security.java

public static String byteToHexChars(final int i) {
    final String s = "0" + Integer.toHexString(i);
    return s.substring(s.length() - 2);
}

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

@Test
public static void testDecodeSingle() {

    final byte[] input = new byte[2];
    for (int i = 0; i < 256; i++) {
        final String hex = Integer.toHexString(i);
        if (hex.length() == 1) {
            input[0] = (byte) '0';
            input[1] = (byte) hex.charAt(0);
        } else {/* ww  w .j a v  a  2  s.  c  o m*/
            input[0] = (byte) hex.charAt(0);
            input[1] = (byte) hex.charAt(1);
        }
        final int decoded = HexDecoder.decodeSingle(input, 0);
        Assert.assertEquals(decoded, i);
    }
}

From source file:Main.java

public static String getMD5String(String str) {
    MessageDigest messageDigest = null;
    try {/*ww  w. j  a v  a 2 s.co m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (Exception e) {
        return null;
    }

    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (byte b : byteArray) {
        if ((0xFF & b) < 0x10)
            md5StrBuff.append("0");
        md5StrBuff.append(Integer.toHexString(0xFF & b));
    }
    return md5StrBuff.toString();
}

From source file:Main.java

/**
 * Encode a string so that it can be safely used as attribute value in
 * XML output.//from   w w  w  . j a va2  s .  c  o  m
 * @param attribute the attribute value to be encoded.
 * @return a string representing the attribute value that can be safely
 *         used in XML output.
 */
public static String attributeEscape(String attribute) {
    final StringBuffer sb = new StringBuffer();
    if (attribute != null) {
        char c;
        final int l = attribute.length();
        for (int i = 0; i < l; i++) {
            c = attribute.charAt(i);
            switch (c) {
            case '<':
                sb.append("&lt;");
                break;
            case '>':
                sb.append("&gt;");
                break;
            case '\'':
                sb.append("&apos;");
                break;
            case '"':
                sb.append("&quot;");
                break;
            case '&':
                sb.append("&amp;");
                break;
            default:
                if (c > Byte.MAX_VALUE || Character.isISOControl(c)) {
                    sb.append("&#x");
                    sb.append(Integer.toHexString(c));
                    sb.append(';');
                } else {
                    sb.append(c);
                }
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash = null;
    try {//w ww .j  a va  2 s .c om
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } 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");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:com.sammyun.util.MD5.java

/**
 * @param str/*ww  w.  j  ava  2 s.  c  om*/
 * @return
 * @throws NoSuchAlgorithmException
 */
public static String crypt(String str) throws NoSuchAlgorithmException {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }

    StringBuffer hexString = new StringBuffer();

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());
    byte[] hash = md.digest();

    for (byte aHash : hash) {
        if ((OXFF & aHash) < OX10) {
            hexString.append("0").append(Integer.toHexString((OXFF & aHash)));
        } else {
            hexString.append(Integer.toHexString(OXFF & aHash));
        }
    }

    return hexString.toString();
}

From source file:AddSHA1.java

public static String bytetoString(byte[] digest) {
    String str = "";
    String tempStr = "";

    for (int i = 1; i < digest.length; i++) {
        tempStr = (Integer.toHexString(digest[i] & 0xff));
        if (tempStr.length() == 1) {
            str = str + "0" + tempStr;
        } else {/*from  www. j  av a 2 s  .c om*/
            str = str + tempStr;
        }
    }
    return str.toLowerCase();
}

From source file:Main.java

public static String md5_32(String plainText) {
    String re_md5 = new String();
    try {/*w  w w .  j a  va2s. co m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.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));
        }

        re_md5 = buf.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return re_md5;
}

From source file:Main.java

/**
 * Returns the contrast ratio between {@code foreground} and {@code background}.
 * {@code background} must be opaque.//from www .  j a v a2 s . com
 * <p>
 * Formula defined
 * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
 */
public static double calculateContrast(int foreground, int background) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException(
                "background can not be translucent: #" + Integer.toHexString(background));
    }
    if (Color.alpha(foreground) < 255) {
        // If the foreground is translucent, composite the foreground over the background
        foreground = compositeColors(foreground, background);
    }

    final double luminance1 = calculateLuminance(foreground) + 0.05;
    final double luminance2 = calculateLuminance(background) + 0.05;

    // Now return the lighter luminance divided by the darker luminance
    return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
}

From source file:Main.java

/**
 * Converts the raw characters to XML escape characters.
 * /*from   ww  w.  j  a  va2s.co m*/
 * @param rawContent
 * @param charset Null when charset is not known, so we assume it's unicode
 * @param isNoLines
 * @return escape string
 */
public static String escapeXML(String rawContent, String charset, boolean isNoLines) {
    if (rawContent == null)
        return ""; //$NON-NLS-1$
    else {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < rawContent.length(); i++) {
            char ch = rawContent.charAt(i);
            if (ch == '\'')
                sb.append("&#39;"); //$NON-NLS-1$
            else if (ch == '&')
                sb.append("&amp;"); //$NON-NLS-1$
            else if (ch == '"')
                sb.append("&quot;"); //$NON-NLS-1$
            else if (ch == '<')
                sb.append("&lt;"); //$NON-NLS-1$
            else if (ch == '>')
                sb.append("&gt;"); //$NON-NLS-1$
            else if (ch > '~' && charset != null && charSetImpliesAscii(charset))
                // TODO - why is hashcode the only way to get the unicode number for the character
                // in jre 5.0?
                sb.append("&#x" + Integer.toHexString(new Character(ch).hashCode()).toUpperCase() + ";"); //$NON-NLS-1$ //$NON-NLS-2$
            else if (isNoLines) {
                if (ch == '\r')
                    sb.append("&#xA;"); //$NON-NLS-1$
                else if (ch != '\n')
                    sb.append(ch);
            } else
                sb.append(ch);
        }
        return sb.toString();
    }
}