This article describes the Java implementation of the binary conversion tool class. Share it for your reference, as follows:
import java.nio.charset.Charset;/** * Hexadecimal (abbreviated as hex or subscript 16) is a carry system in mathematics that goes from 16 to 1. It is generally represented by the numbers 0 to 9 and the letters A to F (where: A~F is 10~15). <br> * For example, the decimal number 57 is written in binary 111001 and the hexadecimal 39 is written in hexadecimal. <br> * In order to distinguish between hexadecimal and decimal values, languages like java and c will prefix 0x in front of the hexadecimal number, for example, 0x20 is 32 in decimal, instead of 20 in decimal<br> * * Reference: https://my.oschina.net/xinxingegeya/blog/287476 * * @author Looly * */public class HexKit { /** * Lowercase character array used to create the output of hexadecimal characters*/ private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * A capital character array used to create the output of hexadecimal characters*/ private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Convert a byte array to a hexadecimal character array* * @param data byte[] * @return Hex char[] */ public static char[] encodeHex(byte[] data) { return encodeHex(data, true); } /** * Convert a byte array to a hexadecimal character array* * @param str string* @param charset Encoding* @return Hex char[] */ public static char[] encodeHex(String str, Charset charset) { return encodeHex(StrKit.getBytes(str, charset), true); } /** * Convert byte array to hexadecimal character array* * @param data byte[] * @param toLowerCase <code>true</code> Transfer to lowercase format, <code>false</code> Transfer to uppercase format* @return Hex char[] */ public static char[] encodeHex(byte[] data, boolean toLowerCase) { return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } /** * Convert byte array to hexadecimal string* * @param data byte[] * @return HexString */ public static String encodeHexStr(byte[] data) { return encodeHexStr(data, true); } /** * Convert byte array to hex string* * @param data byte[] * @param toLowerCase <code>true</code> Transfer to lowercase format, <code>false</code> Transfer to uppercase format* @return HexString */ public static String encodeHexStr(byte[] data, boolean toLowerCase) { return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ decodeHexStr(hexStr.toCharArray(), charset); } /** * Convert hex character array to string* * @param hexData Hex char[] * @param charset Encoding* @return string*/ public static String decodeHexStr(char[] hexData, Charset charset) { return StrKit.str(decodeHex(hexData), charset); } /** * Convert hex character array to byte array* * @param hexData Hex char[] * @return byte[] * @throws RuntimeException If the source hex character array is a strange length, a runtime exception will be thrown*/ public static byte[] decodeHex(char[] hexData) { int len = hexData.length; if ((len & 0x01) != 0) { throw new RuntimeException("Odd number of characters."); } byte[] out = new byte[len >> 1]; // two characters form the hex value. for (int i = 0, j = 0; j < len; i++) { int f = toDigit(hexData[j], j) << 4; j++; f = f | toDigit(hexData[j], j); j++; out[i] = (byte) (f & 0xFF); } return out; } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- toDigits) { return new String(encodeHex(data, toDigits)); } /** * Convert byte array to hex character array* * @param data byte[] * @param toDigits char[] for controlling output * @return hex char[] */ private static char[] encodeHex(byte[] data, char[] toDigits) { int l = data.length; char[] out = new char[l << 1]; // two characters form the hex value. for (int i = 0, j = 0; i < l; i++) { out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; out[j++] = toDigits[0x0F & data[i]]; } return out; } /** * Convert hexadecimal characters into an integer* * @param ch hexadecimal char * @param index The position of hexadecimal characters in the character array* @return An integer* @throws RuntimeException When ch is not a legal hexadecimal character, throw a runtime exception*/ private static int toDigit(char ch, int index) { int digit = Character.digit(ch, 16); if (digit == -1) { throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index); } return digit; } //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- bString.length() % 8 != 0) return null; StringBuffer tmp = new StringBuffer(); int iTmp = 0; for (int i = 0; i < bString.length(); i += 4) { iTmp = 0; for (int j = 0; j < 4; j++) { iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1); } tmp.append(Integer.toHexString(iTmp)); } return tmp.toString(); } /** * HexString * @param hexString * @return */ public static String hex2Binary(String hexString) { if (hexString == null || hexString.length() % 2 != 0) return null; String bString = "", tmp; for (int i = 0; i < hexString.length(); i++) { tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16)); bString += tmp.substring(tmp.length() - 4); } return bString; } /** * Convert binary to hex* @param buf * @return */ public static String binary2Hex(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; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * Convert hexStr to binary* @param hexStr * @return */ public static byte[] hex2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; }}PS: Here are a few online conversion and calculation tools for this website. I believe it will be helpful to you:
Online arbitrary conversion tool:
http://tools.VeVB.COM/transcoding/hexconvert
Online Standard Calculator:
http://tools.VeVB.COM/jisuanqi/jsq
Online scientific calculator:
http://tools.VeVB.COM/jisuanqi/jsqkeexue
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.