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 getMD5(String str) {
    MessageDigest messageDigest = null;
    try {/* www  .  j a  va 2 s .  c  o  m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
    } catch (UnsupportedEncodingException e) {
    }
    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        } else {
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
    }
    return md5StrBuff.substring(8, 24).toString();
}

From source file:Unicode2ASCII.java

public static String toJAVA(String unicode) {
    String tc = unicode;//from   w  w  w.j a  va 2  s. c  o m
    String output = "";
    char[] ca = tc.toCharArray();
    for (int i = 0; i < ca.length; ++i) {
        char a = ca[i];
        if ((int) a > 255) {
            output += "\\u" + Integer.toHexString((int) a);
        } else {
            output += a;
        }
    }
    return output;
}

From source file:Unicode2ASCII.java

public static String toJAVA(String unicode) {
    String output = "";

    char[] charArray = unicode.toCharArray();

    for (int i = 0; i < charArray.length; ++i) {
        char a = charArray[i];
        if ((int) a > 255) {
            output += "\\u" + Integer.toHexString((int) a);
        } else {//from   w w w.j av a2  s .co  m
            output += a;
        }
    }

    return output;
}

From source file:Main.java

public static String md5s(String plainText) {
    String str = "";
    try {/*from  w  ww  .j a v a  2  s .  com*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer 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));
        }
        str = buf.toString();

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }
    return str;
}

From source file:edu.rit.flick.genetics.util.HexPrinter.java

public static String shortToHexString(final short s) {
    return leftPad(Integer.toHexString(s), 4, '0');
}

From source file:Main.java

public static int get_crc16(byte[] bufData, int buflen, byte[] pcrc) {
    int ret = 0;//from   w  w  w  .  j a v a  2  s .c  o  m
    int CRC = 0x0000ffff;
    int POLYNOMIAL = 0x0000a001;
    int i, j;

    if (buflen == 0) {
        return ret;
    }
    for (i = 0; i < buflen; i++) {
        CRC ^= ((int) bufData[i] & 0x000000ff);
        for (j = 0; j < 8; j++) {
            if ((CRC & 0x00000001) != 0) {
                CRC >>= 1;
                CRC ^= POLYNOMIAL;
            } else {
                CRC >>= 1;
            }
        }
        // System.out.println(Integer.toHexString(CRC));
    }

    System.out.println(Integer.toHexString(CRC));
    pcrc[0] = (byte) (CRC & 0x00ff);
    pcrc[1] = (byte) (CRC >> 8);

    return ret;
}

From source file:Main.java

/**
 * Convert decimalism to other./*from   w  w  w .j  a v  a2s .c  o  m*/
 * @param decimal Value in decimalism.
 * @param radix Which system to convert. (Only 2, 8, 16)
 */
public static String toBits(int decimal, int radix) {
    String num;
    switch (radix) {
    case 2:
        num = "00000000000000000000000000000000" + Integer.toBinaryString(decimal);
        return "0b" + num.substring(num.length() - 32);
    case 8:
        num = "00000000000" + Integer.toOctalString(decimal);
        return "0" + num.substring(num.length() - 11);
    case 16:
        num = "00000000" + Integer.toHexString(decimal);
        return "0x" + num.substring(num.length() - 8);
    default:
        return UNKNOWN;
    }
}

From source file:Main.java

/**
 * Format a value as int into a hexadecimal string with a length.
 * //from  ww  w .  jav  a2  s.c o  m
 * @param value
 *            the value to convert
 * @param len
 *            the length of the string
 * @return
 */
public static String format(int value, int len) {
    String result = Integer.toHexString(value);
    int length = result.length();
    // Test if value is negative => FFFF in front of
    for (int i = 0; i < len - length; i++) {
        result = "0" + result;
    }
    if (len < length) {
        result = result.substring(length - len);
    }
    return result.toUpperCase();
}

From source file:Main.java

/**
 * Get the port for mesh usage(For mesh require port hex uppercase).
 * /* w  ww .  j  a v a2 s  .  co m*/
 * @param port the port
 * @return the port for mesh usage
 */
public static String getPortForMesh(int port) {
    String portHexUppercase = Integer.toHexString(port).toUpperCase(Locale.US);
    int numberOfZero = 4 - portHexUppercase.length();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < numberOfZero; i++) {
        sb.append("0");
    }
    sb.append(portHexUppercase);
    return sb.toString();
}

From source file:Main.java

public static String MD5(String string) {
    byte[] hash;//w w  w .ja va2  s.c  o 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();
}