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 toHexString(byte bytes[]) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < bytes.length; ++i) {
        buf.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
    }/*from w ww.  j  ava  2 s  .  c o m*/
    return buf.toString();
}

From source file:com.qtplaf.library.util.NumberUtils.java

/**
 * Converts a byte to an HEX string of two bytes (FF).
 * /*from ww  w  . ja  v a 2  s.  com*/
 * @param b The byte to convert.
 * @return The HEX string.
 */
public static String toHexString(byte b) {
    return StringUtils.leftPad(Integer.toHexString(b & 0xff).toUpperCase(), 2, "0");
}

From source file:com.moki.touch.util.UrlUtil.java

/**
 * This method is used to create a filename for cached content
 * @param url the url of the content to be cached
 * @return an MD5 hashed string// w w w. jav  a2s . com
 */
public static String hashUrl(String url) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(url.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);
        }
        return hexString.toString();

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

From source file:net.hardisonbrewing.signingserver.service.push.PushSIGService.java

public static void register(boolean active) {

    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(BISPushInfo.REGISTER_URL);
    stringBuffer.append("?pin=");
    stringBuffer.append(Integer.toHexString(DeviceInfo.getDeviceId()));
    stringBuffer.append("&active=");
    stringBuffer.append(active);/*w  w w .  j  av a 2  s.  co  m*/

    log.info(stringBuffer.toString());

    ConnectionFactory connectionFactory = new ConnectionFactory();
    ConnectionDescriptor connectionDescriptor = connectionFactory.getConnection(stringBuffer.toString());

    if (connectionDescriptor == null) {
        log.error("Unable to create connection. Push registration not completed.");
        return;
    }

    HttpConnection connection = null;

    try {
        connection = (HttpConnection) connectionDescriptor.getConnection();
        int responseCode = connection.getResponseCode();
        if (responseCode >= 200 && responseCode < 300) {
            log.error("Push registration completed.");
            PushSIGStatusStore.put(active);
            IconService.updateIcon();
        } else {
            log.error("Made connection with server but error[" + responseCode
                    + "] was returned. Push registration not completed.");
        }
    } catch (IOException e) {
        log.error("Exception caught while registering with server. Push registration not completed.");
    } finally {
        IOUtility.safeClose(connection);
    }
}

From source file:com.agiletec.plugins.jpuserreg.aps.system.services.userreg.util.ShaEncoder.java

public static String encodePassword(String password) throws NoSuchAlgorithmException {

    if (password != null) {

        MessageDigest digest = MessageDigest.getInstance("SHA");

        digest.update(password.getBytes());

        byte bytes[] = digest.digest();

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < bytes.length; i++) {

            int b = bytes[i] & 0xff;

            if (b < 16) {

                buffer.append("0");

            }//from   w ww. ja  v a2 s  .c o  m

            buffer.append(Integer.toHexString(b));

        }

        password = buffer.toString();

    }

    return password;

}

From source file:com.github.horrorho.inflatabledonkey.data.blob.BlobA6.java

public BlobA6(ByteBuffer blob) {
    super(blob);//from   w  w  w  . java2 s.co m

    if (type() != 0x000000A6) {
        // Presumed type 0x000000A6, consider throwing exception.
        logger.warn("** RespA6() - unexpected type: 0x{}", Integer.toHexString(type()));
    }

    x = blob.getInt();
    blob.get(tag);
    align(blob);

    list = new BlobLists(blob);
    if (list.size() < 3) {
        throw new IllegalArgumentException("too few blob fields: " + list.size());
    }
}

From source file:Main.java

public static String displayPropertiesToCSS(Font font, Color fg) {
    StringBuffer rule = new StringBuffer("body {");
    if (font != null) {
        rule.append(" font-family: ");
        rule.append(font.getFamily());/*from  w  w  w  . java  2s  .  c  o  m*/
        rule.append(" ; ");
        rule.append(" font-size: ");
        rule.append(font.getSize());
        rule.append("pt ;");
        if (font.isBold()) {
            rule.append(" font-weight: 700 ; ");
        }
        if (font.isItalic()) {
            rule.append(" font-style: italic ; ");
        }
    }
    if (fg != null) {
        rule.append(" color: #");
        if (fg.getRed() < 16) {
            rule.append('0');
        }
        rule.append(Integer.toHexString(fg.getRed()));
        if (fg.getGreen() < 16) {
            rule.append('0');
        }
        rule.append(Integer.toHexString(fg.getGreen()));
        if (fg.getBlue() < 16) {
            rule.append('0');
        }
        rule.append(Integer.toHexString(fg.getBlue()));
        rule.append(" ; ");
    }
    rule.append(" }");
    return rule.toString();
}

From source file:Main.java

/**
 * Escapes a string so that it's safe for use as a file or directory name on at least FAT32
 * filesystems. FAT32 is the most restrictive of all filesystems still commonly used today.
 *
 * <p>For simplicity, this only handles common characters known to be illegal on FAT32:
 * <, >, :, ", /, \, |, ?, and *. % is also escaped since it is used as the escape character.
 * Escaping is performed in a consistent way so that no collisions occur and
 * {@link #unescapeFileName(String)} can be used to retrieve the original file name.
 *
 * @param fileName File name to be escaped.
 * @return An escaped file name which will be safe for use on at least FAT32 filesystems.
 *//*from w w w.  j av  a  2s. c  om*/
public static String escapeFileName(String fileName) {
    int length = fileName.length();
    int charactersToEscapeCount = 0;
    for (int i = 0; i < length; i++) {
        if (shouldEscapeCharacter(fileName.charAt(i))) {
            charactersToEscapeCount++;
        }
    }
    if (charactersToEscapeCount == 0) {
        return fileName;
    }

    int i = 0;
    StringBuilder builder = new StringBuilder(length + charactersToEscapeCount * 2);
    while (charactersToEscapeCount > 0) {
        char c = fileName.charAt(i++);
        if (shouldEscapeCharacter(c)) {
            builder.append('%').append(Integer.toHexString(c));
            charactersToEscapeCount--;
        } else {
            builder.append(c);
        }
    }
    if (i < length) {
        builder.append(fileName, i, length);
    }
    return builder.toString();
}

From source file:Main.java

/**
 * @param data the input data.//from  w ww.  j av  a 2s  .c o m
 * @param byteDelimiter a character to separate bytes or null if none is desired.
 * @return the data as a hex string, optionally delimited by the byteDelimiter.
 */
/* package */
static String convertBytesToHex(final byte[] data, final Character byteDelimiter) {
    final StringBuilder sb = new StringBuilder();
    boolean needsSeparator = false;

    for (final byte datum : data) {
        if (needsSeparator && byteDelimiter != null) {
            sb.append(byteDelimiter);
        }

        // get the unsigned portion
        final int v = datum & 0xFF;

        if (v < 0x10) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(v));
        needsSeparator = true;
    }

    return sb.toString();
}

From source file:de.innovationgate.utils.UIDGenerator.java

/**
 * Generates an MD5 hash from the given string
 * @param hashString/*  w w  w  .j  a v a  2  s.  co m*/
 */
public static String generateMD5Hash(String hashString) {
    MessageDigest md5;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    md5.update(hashString.getBytes());

    byte[] array = md5.digest();
    StringBuffer sb = new StringBuffer();
    for (int j = 0; j < array.length; ++j) {
        int b = array[j] & 0xFF;
        if (b < 0x10)
            sb.append('0');
        sb.append(Integer.toHexString(b));
    }
    return sb.toString();
}