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

/*** this method transfer characters into unicode(U+****U+****...)
 * if bad input ,then return ""**//from  w w w  . ja va  2s .  co  m
 *
 * s_input is a chinese or english string to be translated can not be null
 */
private static String get_cnuni_out(String s_input) {
    int k = s_input.length();
    String res = "", ith = "";
    int ii, m;
    for (int i = 0; i < k; ++i) {
        ith = Integer.toHexString((int) (s_input.charAt(i)));
        m = ith.length();
        for (ii = 0; ii < 4 - m; ii++) {
            ith = "0" + ith;
        }
        res = res + u_big + ith;
    }
    return res;
}

From source file:Main.java

public static String encryptMD5(String password) {
    try {/*from   w  w  w. j av a 2 s  .c  om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        byte byteData[] = md.digest();
        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();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String md5(String str) {
    try {// w ww.j  a v a  2s .co m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());

        byte[] b = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            int v = (int) b[i];
            v = v < 0 ? 0x100 + v : v;
            String cc = Integer.toHexString(v);
            if (cc.length() == 1)
                sb.append('0');
            sb.append(cc);
        }

        return sb.toString();
    } catch (Exception e) {
    }
    return "";
}

From source file:Main.java

public static String sha1(String s) {
    try {/*from ww  w  .ja  v  a2  s .c o  m*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString((0xFF & messageDigest[i]) | 0x100).substring(1));
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:ColorUtils.java

/**
 * Serializes a color to its HTML markup (e.g. "#ff0000" for red)
 * //w w w.  j  a  va2s.  c o  m
 * @param c The color to serialize
 * @return The HTML markup of the color
 */
public static String toHTML(Color c) {
    String ret = "#";
    String hex;
    hex = Integer.toHexString(c.getRed());
    if (hex.length() < 2)
        hex = "0" + hex;
    ret += hex;
    hex = Integer.toHexString(c.getGreen());
    if (hex.length() < 2)
        hex = "0" + hex;
    ret += hex;
    hex = Integer.toHexString(c.getBlue());
    if (hex.length() < 2)
        hex = "0" + hex;
    ret += hex;
    return ret;
}

From source file:Main.java

public static String MD5(String md5) {
    if (md5 == null) {
        return null;
    }/*from w  w w.j ava2s. c  o  m*/
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuilder sb = new StringBuilder();
        for (int a = 0; a < array.length; a++) {
            sb.append(Integer.toHexString((array[a] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String md5(final String s) {
    try {//w  w w .jav  a 2s .c om
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        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);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return "";
}

From source file:Main.java

/**
 * Generate a unicast MAC address./*from w w w .java2  s  . co m*/
 * A unicast MAC address is the one with an even second hex.
 * i.e. x[0,2,4,6,8,A,C,E]:xx:xx:xx:xx:xx
 * 
 * @return Unicast MAC address
 */
public static String generateRandomMACAddress() {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    sb.append(Integer.toHexString(r.nextInt(16)));
    int i = r.nextInt(16);
    while (i % 2 != 0)
        i = r.nextInt(16);
    sb.append(Integer.toHexString(i));
    while (sb.length() <= 12)
        sb.append(Integer.toHexString(r.nextInt()));
    String address = prepareMACAddress(sb.subSequence(0, 12).toString());
    if (address.equals("ff:ff:ff:ff:ff:ff"))
        return generateRandomMACAddress();
    return address;
}

From source file:Main.java

public static String MD5(String text) {
    StringBuffer buffer;//ww w . j av  a  2s .c  om
    MessageDigest digest;
    byte[] data;
    int i, n;

    buffer = new StringBuffer();

    try {
        digest = MessageDigest.getInstance("MD5");
        digest.update(text.getBytes());
        data = digest.digest();

        for (i = 0, n = data.length; i < n; ++i) {
            buffer.append(Integer.toHexString(0xff & data[i]));
        }
    } catch (Exception e) {
    }

    return buffer.toString();
}

From source file:Main.java

public static String crypt(String senha) {
    try {/*  w w  w  . j a v a2s . c  om*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(senha.getBytes());

        byte messageDigest[] = digest.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return senha;
    }
}