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

/**
 * Ensures an arc's label is indeed printable (dot uses US-ASCII). 
 *//*from  ww  w .  j  a  va 2s  .c o  m*/
private static String printableLabel(int label) {
    if (label >= 0x20 && label <= 0x7d) {
        return Character.toString((char) label);
    } else {
        return "0x" + Integer.toHexString(label);
    }
}

From source file:Main.java

/**
 * Write a {@link Color} value into XML output.
 *
 * @param value/*  w  w  w  .  j a  v  a 2s. c  o m*/
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printColor(Color value) {
    if (value == null) {
        throw new IllegalArgumentException("The provided color is invalid!");
    }

    String r = Integer.toHexString(value.getRed()).trim();
    if (r.length() == 1)
        r = "0" + r;
    String g = Integer.toHexString(value.getGreen()).trim();
    if (g.length() == 1)
        g = "0" + g;
    String b = Integer.toHexString(value.getBlue()).trim();
    if (b.length() == 1)
        b = "0" + b;
    return "#" + r + g + b;
}

From source file:Main.java

/**
 * Convert a color-int into a color hexadecimal string.
 * Format will be #AARRGGBB if includeAlpha is true, or #RRGGBB if is false.
 *
 * @param color        color int//from  ww w.  j a  va  2  s . co  m
 * @param includeAlpha if true hexadecimal will include alpha channel
 * @return color code for the color-int specified and based on the includeAlpha
 */
public static String colorToString(int color, boolean includeAlpha) {
    if (includeAlpha) {
        return "#" + Integer.toHexString(color);
    } else {
        return "#" + Integer.toHexString(color).substring(2);
    }
}

From source file:Main.java

/**
 * parse "24,-2,52,-102,-93,-60" to "18,fe,34,9a,a3,c4"
 * parse the bssid from hex to String/*w  ww.j  ava  2  s .co m*/
 * @param bssidBytes the hex bytes bssid, e.g. {24,-2,52,-102,-93,-60}
 * @return the String of bssid, e.g. 18fe349aa3c4
 */
public static String parseBssid(byte[] bssidBytes) {
    StringBuilder sb = new StringBuilder();
    int k;
    String hexK;
    String str;
    for (int i = 0; i < bssidBytes.length; i++) {
        k = 0xff & bssidBytes[i];
        hexK = Integer.toHexString(k);
        str = ((k < 16) ? ("0" + hexK) : (hexK));
        System.out.println(str);
        sb.append(str);
    }
    return sb.toString();
}

From source file:Main.java

public static String SHA1(String str) {
    try {//from   w  w  w. j  a  v  a 2  s .  co  m
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(str.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();

        for (byte i : messageDigest) {
            String shaHex = Integer.toHexString(i & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();

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

From source file:Main.java

/**
 * SHA256 implementation/*from   w w  w  .  j  av  a 2 s. c  o  m*/
 * @param toHash the cleartext string
 * @return a SHA256 hashed string
 */
public static String sha256(final String toHash) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(toHash.getBytes());
        StringBuilder hexString = new StringBuilder();

        for (byte aHash : hash) {
            String hex = Integer.toHexString(0xff & aHash);
            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

/**
 * Converts byte data to a Hex-encoded string.
 *
 * @param data/*from w  w w .  j a  v a  2s  .  c o m*/
 *            data to hex encode.
 *
 * @return hex-encoded string.
 */
public static String toHex(byte[] data) {
    StringBuilder sb = new StringBuilder(data.length * 2);
    for (int i = 0; i < data.length; i++) {
        String hex = Integer.toHexString(data[i]);
        if (hex.length() == 1) {
            // Append leading zero.
            sb.append("0");
        } else if (hex.length() == 8) {
            // Remove ff prefix from negative numbers.
            hex = hex.substring(6);
        }
        sb.append(hex);
    }
    return sb.toString().toLowerCase(Locale.getDefault());
}

From source file:Main.java

public static final String bytesToHexString(byte[] bArray) {
    StringBuffer sb = new StringBuffer(bArray.length);
    String sTemp;// ww  w . j  ava 2  s  .co m
    for (int i = 0; i < bArray.length; i++) {
        sTemp = Integer.toHexString(0xFF & bArray[i]);
        if (sTemp.length() < 2)
            sb.append(0);
        sb.append(sTemp.toUpperCase());
    }
    return sb.toString();
}

From source file:Main.java

private static String hashBytes(MessageDigest hash, byte[] bytes) {
    hash.update(bytes);//from  w  w  w  .j  a  v  a2 s  .c om
    byte[] digest = hash.digest();
    StringBuilder builder = new StringBuilder();
    for (int b : digest) {
        builder.append(Integer.toHexString((b >> 4) & 0xf));
        builder.append(Integer.toHexString((b >> 0) & 0xf));
    }
    return builder.toString();
}

From source file:Main.java

public static String sha1(String strSrc) {
    MessageDigest md = null;//  www.j  ava2 s .c  o  m
    String strDes = "";

    byte[] bt = strSrc.getBytes();
    try {
        md = MessageDigest.getInstance("SHA-1");
        md.update(bt);
        byte[] encryptStr = md.digest();
        String tmp = null;
        for (int i = 0; i < encryptStr.length; i++) {
            tmp = (Integer.toHexString(encryptStr[i] & 0xFF));
            if (tmp.length() == 1) {
                strDes += "0";
            }
            strDes += tmp;
        }
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Invalid algorithm.");
        return null;
    }
    return strDes;
}