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

private static String byteArrayToHexString(byte[] b) {
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++) {
        int v = b[i] & 0xff;
        if (v < 16) {
            sb.append('0');
        }//from  w  ww.j  ava2  s.com
        sb.append(Integer.toHexString(v));
    }
    return sb.toString().toUpperCase();
}

From source file:Main.java

public static String calculateMD5(String string, String encoding) {
    try {/*w w  w. j a  v a2 s .c  o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(string.getBytes(Charset.forName(encoding)));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String md5(String input) {
    String res = "";
    try {//from  ww w  .ja va  2 s.c om
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
    }
    return res;
}

From source file:Main.java

public static void checkGlError(String glOp) {
    int error = GLES20.glGetError();
    if (GLES20.GL_NO_ERROR != error) {
        String errorStr = GLU.gluErrorString(error);
        if (null == errorStr) {
            errorStr = GLUtils.getEGLErrorString(error);
        }//from w ww  .ja  v  a2 s.  com

        String msg = glOp + " caused GL error 0x" + Integer.toHexString(error) + ":" + errorStr;
        throw new GLException(error, msg);
    }
}

From source file:Main.java

public static String encodeEntity(int entity) {
    if (entities.containsKey(entity))
        return entities.get(entity);
    if (entity < 128)
        return Character.toString((char) entity);
    if (entity < 10000)
        return "&#" + entity + ";";
    else//from   ww  w  . ja va  2s.  c  o  m
        return "&#x" + Integer.toHexString(entity) + ";";
}

From source file:Main.java

public static String signature(String source) {

    try {/*from w w w.  jav a  2 s .c  om*/
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.reset();
        md.update(source.getBytes());

        byte[] mdbytes = md.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            String hex = Integer.toHexString(0xff & mdbytes[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();

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

From source file:Main.java

private static String getMd5(String md5) throws Exception {
    MessageDigest md = java.security.MessageDigest.getInstance(MD5);
    byte[] array = md.digest(md5.getBytes(CHARSET_NAME));
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }/*from ww  w .  ja  va2 s  . c  o  m*/
    return sb.toString();
}

From source file:Main.java

public static final String toHexString(byte[] paramArrayOfByte) {
    int i = paramArrayOfByte.length * 2;
    StringBuffer localStringBuffer1 = new StringBuffer(i);
    int j = 0;/*from   w  w  w .ja va2 s  .c o m*/
    while (true) {
        int k = paramArrayOfByte.length;
        if (j >= k)
            return localStringBuffer1.toString().toLowerCase();
        int m = paramArrayOfByte[j] & 0xFF;
        if (m < 16)
            localStringBuffer1.append(48);
        String str = Integer.toHexString(m);
        StringBuffer localStringBuffer3 = localStringBuffer1.append(str);
        j += 1;
    }
}

From source file:Main.java

public static String MD5(final String s) {
    final String MD5 = "MD5";
    try {//from ww w .j  a v  a  2s . co m
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

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

From source file:Main.java

public static String byte2hex(byte[] buffer, int len) {
    String h = "";

    for (int i = 0; i < len; i++) {
        String temp = Integer.toHexString(buffer[i] & 0xFF);
        if (temp.length() == 1) {
            temp = "0" + temp;
        }//from   w  w w .  j  a  v  a2  s. c  o  m
        h = h + " " + temp;
    }

    return h;
}