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

public static String bytesToHexString(byte[] src) {
    StringBuilder stringBuilder = new StringBuilder();
    if (src == null || src.length <= 0) {
        return null;
    }//  w w w.j  av  a  2 s . c o  m
    for (int i = 0; i < src.length; i++) {
        int v = src[i] & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
    }
    return stringBuilder.toString();
}

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

/**
 * Returns the hashed value of <code>clear</code>.
 *//*from   w w w .ja  v a 2  s .  c o m*/
public static String hash(String clear) throws Exception {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] b = md.digest(clear.getBytes());

        int size = b.length;
        StringBuffer h = new StringBuffer(size);
        for (int i = 0; i < size; i++) {
            int u = b[i] & 255; // unsigned conversion
            if (u < 16) {
                h.append("0" + Integer.toHexString(u));
            } else {
                h.append(Integer.toHexString(u));
            }
        }
        return h.toString();
    } catch (Exception e) {
        throw new Exception(e);
    }
}

From source file:Main.java

public static int prefixCodedToInt(final String prefixCoded) {
    final int shift = prefixCoded.charAt(0) - SHIFT_START_INT;
    if (shift > 31 || shift < 0)
        throw new NumberFormatException(
                "Invalid shift value in prefixCoded string (is encoded value really an INT?)");
    int sortableBits = 0;
    for (int i = 1, len = prefixCoded.length(); i < len; i++) {
        sortableBits <<= 7;//from   ww w .  j a  va  2 s  . c  o  m
        final char ch = prefixCoded.charAt(i);
        if (ch > 0x7f) {
            throw new NumberFormatException("Invalid prefixCoded numerical value representation (char "
                    + Integer.toHexString(ch) + " at position " + i + " is invalid)");
        }
        sortableBits |= ch;
    }
    return (sortableBits << shift) ^ 0x80000000;
}

From source file:com.yoho.core.trace.DefaultSpanNamer.java

private static boolean isDefaultToString(Object delegate, String spanName) {
    return (delegate.getClass().getName() + "@" + Integer.toHexString(delegate.hashCode())).equals(spanName);
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(text.getBytes());//ww w .j  ava 2  s.c  o m

    byte byteData[] = md.digest();

    //convert the byte to hex format method 1
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }

    //System.out.println("Hex format : " + sb.toString());

    //convert the byte to hex format method 2
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        String hex = Integer.toHexString(0xff & byteData[i]);
        if (hex.length() == 1)
            hexString.append('0');
        hexString.append(hex);
    }
    return hexString.toString();
}

From source file:Main.java

public static String calcHash(File file, String algo) throws Exception {
    MessageDigest digester = MessageDigest.getInstance(algo);

    FileInputStream is = new FileInputStream(file);
    DigestInputStream dis;/*from   ww  w  .j  av  a  2  s .co m*/
    try {
        dis = new DigestInputStream(is, digester);

        for (byte[] buffer = new byte[1024 * 4]; dis.read(buffer) >= 0;) {
            // just read it
        }
    } finally {
        is.close();
    }

    byte[] digest = digester.digest();

    StringBuffer hash = new StringBuffer(digest.length * 2);

    for (int i = 0; i < digest.length; i++) {
        int b = digest[i] & 0xFF;

        if (b < 0x10) {
            hash.append('0');
        }

        hash.append(Integer.toHexString(b));
    }

    return hash.toString();
}

From source file:Main.java

/**
 * <p>Converts the string to the unicode format '\u0020'.</p>
 * /*from   ww  w. j av  a2 s.com*/
 * <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(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

/**
 * Checks to see if a GLES error has been raised.
 *///www  .  j  a v a2s  . c o  m
public static void checkGlError(String op) {
    int error = GLES20.glGetError();
    if (error != GLES20.GL_NO_ERROR) {
        String msg = op + ": glError 0x" + Integer.toHexString(error);
        Log.e(TAG, msg);
        throw new RuntimeException(msg);
    }
}

From source file:license.mac.MacTest.java

private static String getMACAddress(InetAddress ia) throws Exception {
    //???mac?mac?byte
    byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

    //??mac??String
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < mac.length; i++) {
        if (i != 0) {
            sb.append("-");
        }// ww w .  java 2 s  . c  om
        //mac[i] & 0xFF byte
        String s = Integer.toHexString(mac[i] & 0xFF);
        sb.append(s.length() == 1 ? 0 + s : s);
    }

    //???mac?
    return sb.toString().toUpperCase();
}

From source file:Main.java

private static final String uuidStringFromUuid16(int uuid16) {

    StringBuilder b = new StringBuilder();
    String hex = Integer.toHexString(uuid16 & MASK_UUID16);
    b.append("00000000".substring(hex.length()));
    b.append(hex);//from   ww  w.jav a  2s  . c  o  m
    b.append('-');
    b.append(UUID_BASE);

    return b.toString();
}