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

/**
 * Generates a random byte and returns it as a hexadecimal string.
 *
 * @return A random byte as hexadecimal string.
 *//*from   ww  w. ja va 2  s  .c  o  m*/
public static String generatedRandomByteAsHexString() {
    if (mRandom == null) {
        mRandom = new Random(new Date().getTime());
    }

    int randomInt8 = mRandom.nextInt(256);
    return Integer.toHexString(randomInt8);
}

From source file:Main.java

public static String MD5(String s) {
    try {//from  www.j av a 2  s.c  om
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(s.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuilder buf = new StringBuilder("");
        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));
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "null";
}

From source file:Main.java

/**
 * Returns a String Object which contains the SHA256 Hash value of the input
 *
 * @param input a String object for which SHA256 should be calculated
 * @return SHA256 of the input String/*from w  w w .  j a va 2 s  . co m*/
 */
public static String getSHA256(String input) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(input.getBytes("UTF-8"));
        StringBuilder hexString = new StringBuilder();

        for (int i = 0; i < hash.length; i++) {
            String hex = Integer.toHexString(0xff & hash[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static int checkGlError(boolean throwException) {
    int error = glGetError();
    if (error != GL_NO_ERROR) {
        Log.d(s_LOG_TAG, "GL error = 0x" + Integer.toHexString(error));
        if (throwException) {
            throw new RuntimeException("GL ERROR = 0x" + Integer.toHexString(error));
        }//w ww. j av  a 2s . c o m
    }

    return error;
}

From source file:Main.java

private static StringBuffer getMd5Buffer(String text) {
    StringBuffer buf = new StringBuffer();
    try {/*from  w  ww .j ava 2s .c om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes());
        byte[] b = md.digest();
        int i;
        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));
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return buf;
}

From source file:Main.java

public static String MD5(String md5) {
    if (md5 == null) {
        return null;
    }/*  w  w w.  ja  va 2s  . c o  m*/
    try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte anArray : array) {
            sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        Log.e("tmessages", e.getMessage());
    }
    return null;
}

From source file:Main.java

/**
 * Return the hexadecimal format of a plain text string.
 *
 * @param strValue//from  w w  w  . j  a  va2s.  c  om
 * @return
 */
public static String stringToHex(String strValue) {
    byte byteData[] = null;
    int intHex = 0;
    String strHex = "";
    String strReturn = "";
    try {
        byteData = strValue.getBytes("ISO8859-1");
        for (int i = 0; i < byteData.length; i++) {
            intHex = (int) byteData[i];
            if (intHex < 0)
                intHex += 256;
            if (intHex < 16)
                strHex += "0" + Integer.toHexString(intHex).toUpperCase();
            else
                strHex += Integer.toHexString(intHex).toUpperCase();
        }
        strReturn = strHex;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return strReturn;
}

From source file:Main.java

static public String md5(String str) {
    MessageDigest algorithm = null;
    try {// www  . ja  v  a  2  s .com
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (algorithm != null) {
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            if (Integer.toHexString(0xFF & b).length() == 1)
                hexString.append("0").append(Integer.toHexString(0xFF & b));
            else
                hexString.append(Integer.toHexString(0xFF & b));
        }
        return hexString.toString();
    }
    return "";

}

From source file:Main.java

public static String byteToString(byte b) {
    String s = Integer.toHexString(b & 0xff).toUpperCase();
    if (s.length() > 1) {
        return s;
    } else {/*from  www .jav a2  s. co  m*/
        return "0" + s;
    }
}

From source file:Main.java

public static String hexString(byte[] bytes) {
    StringBuilder val = new StringBuilder();
    for (byte b : bytes) {
        if (b <= 15)
            val.append("0");
        val.append(Integer.toHexString(b & 0xFF));
    }/*  w  w  w.  j  av a  2  s .c  o  m*/
    return val.toString().toLowerCase(Locale.US);
}