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 MD5(String source) {
    String resultHash = null;//from w  w w  .  j ava 2s.  c  o  m
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(source.getBytes("UTF-8"));
        byte[] result = md5.digest();
        StringBuffer buf = new StringBuffer(result.length * 2);
        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal));
        }
        resultHash = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultHash.toString();
}

From source file:Main.java

/**
 * Returns a 32 chararacter hexadecimal representation of an MD5 hash of the
 * given String./*from   w w  w  . j  a  va 2 s .  com*/
 * 
 * @param s the String to hash
 * @return the md5 hash
 */
public final static String md5(final String s) {
    try {
        final byte[] mBytes = mDigest.digest(s.getBytes("UTF-8"));
        final StringBuilder mBuilder = new StringBuilder(32);
        for (final byte aByte : mBytes) {
            final String mHex = Integer.toHexString(aByte & 0xFF);
            if (mHex.length() == 1) {
                mBuilder.append('0');
            }
            mBuilder.append(mHex);
        }
        return mBuilder.toString();
    } catch (final UnsupportedEncodingException ignored) {
    }
    return null;
}

From source file:Main.java

/**
 * for the reason, we use some bit to differ whether it is softap or sta. so, we may get the polluted bssid.
 * /*from   w  w  w .j  a  va2s. c  om*/
 * softap bssid: 1a:fe:34:77:c0:00 sta bssid: 18:fe:34:77:c0:00
 * 
 * @param BSSID
 * @return the real BSSID(SoftAp's BSSID)
 */
public static String restoreSoftApBSSID(String BSSID) {
    String pollutedBitStr = BSSID.substring(1, 2);
    Integer pollutedBitInt = 0;
    // get pollutedBitInt according to 0x..
    pollutedBitInt = Integer.parseInt(pollutedBitStr, 16);
    Integer cleanBitInt = pollutedBitInt | 0x02;
    String cleanBitStr = Integer.toHexString(cleanBitInt);
    return BSSID.substring(0, 1) + cleanBitStr + BSSID.substring(2);
}

From source file:Main.java

public static void appendComponent(StringBuffer buffer, int component) {
    String hex = Integer.toHexString(component);
    if (hex.length() == 1) {
        buffer.append('0');
    }/*  ww w  .ja  v  a 2  s . c  om*/
    buffer.append(hex);
}

From source file:Main.java

public static final String hash(final String s, final String algorithm) {
    try {//from w  w w .  j a va  2 s  . c o  m
        // Create MD5 or SHA-256 Hash
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        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 md5(String str) {
    MessageDigest algorithm = null;
    try {/*w ww . ja  v  a  2s .c  om*/
        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) {
            hexString.append(Integer.toHexString(0xFF & b));
        }
        return hexString.toString();
    }
    return "";

}

From source file:Main.java

public static String hashMD5Generate(String s) {
    try {//from www .  j  av  a  2 s .  com
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        Log.d(DEBUG, hexString.toString());
        return hexString.toString();

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

From source file:Main.java

static public String toHexString(byte[] ba) {
    StringBuffer sbuf = new StringBuffer();
    for (byte b : ba) {
        String s = Integer.toHexString((int) (b & 0xff));
        if (s.length() == 1) {
            sbuf.append('0');
        }/*w w  w  .  j a  va 2s  .c  o m*/
        sbuf.append(s);
    }
    return sbuf.toString();
}

From source file:Main.java

public static String MD5(String md5) {
    try {//  w  w w . j  av  a2s  .  c om
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array;
        try {
            array = md.digest(md5.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            array = md.digest(md5.getBytes());
        }
        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 (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String convertStringToMd5(String valor) {

    MessageDigest mDigest;//from w ww . jav  a  2 s  .c o m
    StringBuffer sb;

    if (valor == "") {
        return null;
    }
    try {
        mDigest = MessageDigest.getInstance("MD5");
        byte[] valorMD5 = mDigest.digest(valor.getBytes("UTF-8"));
        sb = new StringBuffer();

        for (byte b : valorMD5) {
            sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        Logger.getLogger(MessageDigest.class.getName()).log(Level.SEVERE, null, ex);
        Logger.getLogger(StringBuffer.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}