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 bytes2HexString(byte[] paramArrayOfByte) {
    String str1 = "";
    for (int i = 0;; i++) {
        if (i >= paramArrayOfByte.length) {
            return str1;
        }/*from ww w  .  j a  v  a 2s  .  c  o  m*/
        String str2 = Integer.toHexString(0xFF & paramArrayOfByte[i]);
        if (str2.length() == 1) {
            str2 = '0' + str2;
        }
        str1 = str1 + str2;
    }
}

From source file:Main.java

public static String bytesToHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length);
    String temp;//from  www  .  jav a  2  s.  com
    for (int i = 0; i < bytes.length; i++) {
        temp = Integer.toHexString(0xFF & bytes[i]);
        if (temp.length() < 2)
            sb.append(0);
        sb.append(temp.toUpperCase()).append(" ");
    }
    return sb.toString();
}

From source file:Main.java

public static String encodeUnicode(String str) {
    char[] utfBytes = str.toCharArray();
    StringBuffer buffer = new StringBuffer();
    for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
        String hexB = Integer.toHexString(utfBytes[byteIndex]);
        if (hexB.length() <= 2) {
            hexB = "00" + hexB;
        }//from   w  ww . j av  a  2s. c o m
        buffer.append("\\u" + hexB);
    }
    return buffer.substring(0);
}

From source file:Main.java

public static String bytesToHexString(byte[] src) {
    if (src == null || src.length <= 0) {
        return null;
    }/*from  w w  w. j  av  a2 s.c o  m*/
    String hexStr = "";
    for (byte b : src) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        hexStr += hex.toUpperCase(Locale.getDefault());
    }
    return hexStr;
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String parseByte2HexStr(byte buf[]) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }/*  www.ja  v a 2 s  . c o  m*/
        sb.append(hex.toUpperCase());
    }
    return sb.toString();
}

From source file:Main.java

static String HexForByte(byte b) {
    String Hex = Integer.toHexString((int) b & 0xff);
    boolean hasTwoDigits = (2 == Hex.length());
    if (hasTwoDigits)
        return Hex;
    else//from   ww w  .j  a  va2 s  .  c  o m
        return "0" + Hex;
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String byteToHexString(byte[] b) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }//from   ww w . j a va  2  s. com
        hexString.append(hex.toUpperCase());
    }
    return hexString.toString();
}

From source file:Main.java

public static String toHexString(byte[] bytes) {
    StringBuilder hexString = new StringBuilder();
    for (byte b : bytes) {
        String str = Integer.toHexString(b & MotionEventCompat.ACTION_MASK);
        while (str.length() < 2) {
            str = "0" + str;
        }//from www  .  ja  v  a 2s  .c om
        hexString.append(str);
    }
    return hexString.toString();
}

From source file:Main.java

/**
 * Convert byte to Hex String//from w  w  w.ja  v  a2  s .  c o m
 * 
 * @param b
 *            the byte to be converted
 * @return the Hex String
 */
public static String convertByte2HexString(byte b) {
    char u8 = convertByte2Uint8(b);
    return Integer.toHexString(u8);
}

From source file:Main.java

/**
 *
 * @param len/*  w  w  w .  ja v  a2s  .  c  om*/
 * @return
 */
public static String getRandomHex(int len) {
    Random r = new Random();
    StringBuffer sb = new StringBuffer();
    while (sb.length() < len) {
        sb.append(Integer.toHexString(r.nextInt()));
    }
    return sb.toString().substring(0, len).toUpperCase();
}