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

/**
 * Get the ip address for mesh usage(For mesh require the ip address hex uppercase without ".".
 * /*from  w  ww.  ja  v a2 s  . c  o  m*/
 * @param hostname the ip address, e.g. 192.168.1.2
 * @return ip address by hex without ".", e.g. C0A80102
 */
public static String getIpAddressForMesh(String hostname) {
    StringBuilder sb = new StringBuilder();
    String[] segments = hostname.split("\\.");
    int segment;
    String segmentHexStr;
    for (int i = 0; i < segments.length; i++) {
        // get the integer
        segment = Integer.parseInt(segments[i]);
        // transform the integer to hex
        segmentHexStr = Integer.toHexString(segment);
        // transform the hex string to uppercase
        segmentHexStr = segmentHexStr.toUpperCase(Locale.US);
        // append segmentHexStr to the sb
        if (segmentHexStr.length() == 1) {
            sb.append("0");
            sb.append(segmentHexStr);
        } else if (segmentHexStr.length() == 2) {
            sb.append(segmentHexStr);
        } else {
            throw new RuntimeException();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String toUtf8String(String s) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c >= 0 && c <= 255) {
            sb.append(c);//  ww w  .j  a  va  2s. co m
        } else {
            byte[] b;
            try {
                b = String.valueOf(c).getBytes("utf-8");
            } catch (Exception ex) {
                System.out.println(ex);
                b = new byte[0];
            }
            for (int j = 0; j < b.length; j++) {
                int k = b[j];
                if (k < 0)
                    k += 256;
                sb.append("%" + Integer.toHexString(k).toUpperCase());
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String encryption(String plainText) {
    String re_md5 = "";
    try {/* www. j  a v  a 2 s  . co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i = 0;

        StringBuilder sb = new StringBuilder("");
        for (int offset = 0, len = b.length; offset < len; offset++) {
            i = b[offset];
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(i));
        }
        re_md5 = sb.toString();

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

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;/*from   w  ww  .j  a va 2  s  .  c  om*/

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;//w  w w.j  a  va 2s .co m

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }

    return hex.toString();
}

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;/*from www.j a  v a  2  s  .  co m*/

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }

    return hex.toString().substring(8, 24);
}

From source file:Main.java

public static String[] convertToARGB(int color) {
    String alpha = Integer.toHexString(Color.alpha(color)).toUpperCase(Locale.getDefault());
    String red = Integer.toHexString(Color.red(color)).toUpperCase(Locale.getDefault());
    String green = Integer.toHexString(Color.green(color)).toUpperCase(Locale.getDefault());
    String blue = Integer.toHexString(Color.blue(color)).toUpperCase(Locale.getDefault());

    // we want the strings with a leading 0 if needed
    return new String[] { ("00" + alpha).substring(alpha.length()), ("00" + red).substring(red.length()),
            ("00" + green).substring(green.length()), ("00" + blue).substring(blue.length()) };
}

From source file:Componentes.EncryptionMD5.java

public static String toHexadecimal(byte[] digest) {
    String hash = "";
    for (byte aux : digest) {
        int b = aux & 0xff;
        if (Integer.toHexString(b).length() == 1)
            hash += "0";
        hash += Integer.toHexString(b);

    }//from  w w w  . ja  v a  2  s.co m
    return hash;
}

From source file:Main.java

private static String md5(final String s) {
    final String MD5 = "MD5";
    try {//from w  w w.j a va 2  s . co m
        MessageDigest digest = 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 toHexString(Object object) {
    if (object == null) {
        return null;
    }//from  w  w  w  .j av a  2  s  .  com

    return Integer.toHexString(object.hashCode());
}