Java Hex to String convertHexStr(String inStr)

Here you can find the source of convertHexStr(String inStr)

Description

convert Hex Str

License

Apache License

Declaration

public static String convertHexStr(String inStr) 

Method Source Code

//package com.java2s;
/*/*from w w w. ja  v  a 2 s . c om*/
 * BJAF - Beetle J2EE Application Framework
 * ???J2EE???????????
 * ??????2003-2015 ??? (www.beetlesoft.net)
 * 
 * ??????????????????
 *<http://www.apache.org/licenses/LICENSE-2.0>
 *????????????????????????
 *
 * ???????????????????????????????
 * ??? <yuhaodong@gmail.com/>.
 */

public class Main {
    private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    public static String convertHexStr(String inStr) {
        StringBuffer sb = new StringBuffer();
        char[] myBuffer = inStr.toCharArray();
        for (int i = 0; i < inStr.length(); i++) {
            short s = (short) myBuffer[i];
            String hexS = Integer.toHexString(s).toUpperCase();
            if (hexS.length() == 8) {
                sb.append("&#x" + hexS.substring(4) + ";");
            } else {
                sb.append("&#x" + hexS + ";");
            }
        }
        return sb.toString();
    }

    /**
     * Returns a hex string representation of a 8 bit integer. Very fast.
     * Returned hex values are in uppercase.
     * 
     * @param b
     *            byte to convert to hex string
     * 
     * @return hex value
     */
    public static String toHexString(int b) {
        char[] digits = new char[2];
        b = b & 255;

        digits[0] = hexDigits[b / 0x10];
        digits[1] = hexDigits[b % 0x10];

        return new String(digits);
    }
}

Related

  1. convertHexString(String ss)
  2. convertHexStringToString(String hexString)
  3. convertHexToString(String hex)
  4. formatHex(byte[] buf)