There are many ways to convert the binary system in Java. Among them, there are packaging classes for common basic binary octal decimal hexadecimal, etc., which do not need to be implemented through two-out algorithms. The details are as follows:
First of all, the simplest binary conversion methods are:
Convert decimal to hexadecimal:
String Integer.toHexString(int i)
Convert decimal to octal
String Integer.toOctalString(int i)
Convert decimal to binary
String Integer.toBinaryString(int i)
Convert hexadecimal to decimal
Integer.valueOf("FFFF",16).toString() //Cannot handle prefix 0x
Convert octal to decimal
Integer.valueOf("76", 8).toString() //Prefix 0 can be processed to be binary to decimal
Integer.valueOf("0101",2).toString()
Is there any way to directly convert 2, 8, hexadecimal to decimal?
java.lang.Integer class
parseInt(String s, int radix)
Use the cardinality specified by the second parameter to parse the string parameter into a signed integer.
examples from jdk:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10)throwsa NumberFormatException
parseInt("99", throwsa NumberFormatException
parseInt("Kona", 10)throwsa NumberFormatException
parseInt("Kona", 27) returns 411787
How to write a binary conversion (two, eight, sixteen) without algorithm
Integer.toBinaryString
Integer.toOctalString
Integer.toHexString
Then introduce the conversion of byte and hexadecimal numbers in java
Principle analysis:
The byte in Java is composed of 8 bits, and the hexadecimal is the state in 16, which is represented by 4 bits because 24=16. So we can convert a byte into two hexadecimal characters, that is, convert the high 4 and low 4 digits into the corresponding hexadecimal characters, and combine these two hexadecimal strings to obtain the hexadecimal string of byte. Similarly, the opposite conversion also converts two hexadecimal characters into one byte.
In Java, there are two main ideas for converting bytes and hexadecimal:
1. When the binary byte is converted to hexadecimal, perform the "&" operation of the byte high and 0xF0, and then shift the left by 4 bits to obtain the hexadecimal A of the byte high; perform the "&" operation of the byte low and 0x0F to obtain the hexadecimal B of the low and digits, and assemble two hexadecimal numbers into a piece of AB, which is the hexadecimal representation of the byte.
2. When hexadecimal to binary bytes, move the decimal number corresponding to the hexadecimal character to the right 4 to get the byte high A; do the "|" operation of the decimal number B and A of the hexadecimal character corresponding to the low byte to get the hexadecimal binary byte representation
One of the conversion functions is as follows:
/** * * @param bytes * @return Convert binary to hexadecimal character output */ </span> private static String hexStr = "0123456789ABCDEF"; //Global public static String BinaryToHexString(byte[] bytes){ String result = ""; String hex = ""; for(int i=0;i<bytes.length;i++){ //Bytes are 4 bits higher<strong>hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4)); </strong> //Bytes are 4 bits lower<strong>hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F)); </strong> result +=hex; } return result; } /** * * @param hexString * @return Convert hexadecimal to byte array*/ public static byte[] HexStringToBinary(String hexString){ //The length of hexString is rounded by 2, as the length of bytes int len = hexString.length()/2; byte[] bytes = new byte[len]; byte high = 0;//The byte high four-bit byte low = 0;//The byte low four-bit for(int i=0;i<len;i++){ //Transfer to the right four bits to get the high bit high = (byte)((hexStr.indexOf(hexString.charAt(2*i)))<<4); low = (byte)hexStr.indexOf(hexString.charAt(2*i+1)); bytes[i] = (byte) (high|low);//High status or operation} return bytes; } } There is also a similar method:
<span style="font-size:14px;">* Convert byte[] to hex string. Here we can convert byte to int, and then use Integer.toHexString(int) to convert to hex string.
* @param src byte[] data * @return hex string */ public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * Convert hex string to byte[] * @param hexString the hex string * @return byte[] */ public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } /** * Convert char to byte * @param c char * @return byte */ private byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } </span>The two methods are similar, please note here
The above is a string that converts byte[] to hexadecimal. Note that b[i] & 0xFF performs a calculation of a byte and 0xFF, and then uses Integer.toHexString to obtain the hexadecimal string. You can see that
b[ i ] & 0xFF operation is still an int, so why do you need to perform an int operation with 0xFF? Direct Integer.toHexString(b[ i ]);, can't force byte to int be converted? The answer is no.
The reason is:
1. The size of byte is 8 bits and the size of int is 32 bits
2.java's two-part system uses complement
Therefore, when negative numbers are & with negative numbers, the negative numbers will automatically fill up 1, which will lead to errors
0xff is plastic surgery by default, so when a byte and 0xff are involved, the byte will first convert the byte into a plastic surgery operation. In this way, the 24 bits in the result will always be cleared by 0, so the result is always what we want.
There are some online summary methods:
Convert string to hexadecimal string method 1:
/** * Convert string into hexadecimal string*/ public static String str2HexStr(String str) { char[] chars = "0123456789ABCDEF".toCharArray(); StringBuilder sb = new StringBuilder(""); byte[] bs = str.getBytes(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append(chars[bit]); bit = bs[i] & 0x0f; sb.append(chars[bit]); } return sb.toString(); }Convert hexadecimal string into array method 1:
/** * Convert hexstring into byte array* @param hexString * @return byte[] */ public static byte[] hexStringToByte(String hex) { int len = (hex.length() / 2); byte[] result = new byte[len]; char[] achar = hex.toCharArray(); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); } return result; } private static int toByte(char c) { byte b = (byte) "0123456789ABCDEF".indexOf(c); return b; }Convert array to hexadecimal string method 1:
/** * Convert array into hex string* @param byte[] * @return HexString */ public static final String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(bArray.length); String sTemp; 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(); }Method 2 of converting the byte[] array into hexadecimal string:
/** * Convert the array into hex string* @param byte[] * @return HexString */ public static String toHexString1(byte[] b){ StringBuffer buffer = new StringBuffer(); for (int i = 0; i < b.length; ++i){ buffer.append(toHexString1(b[i])); } return buffer.toString(); } public static String toHexString1(byte b){ String s = Integer.toHexString(b & 0xFF); if (s.length() == 1){ return "0" + s; }else{ return s; } }Method 1 of hexadecimal string conversion string:
/** * Convert hexString into string* @param hexString * @return String */ public static String hexStr2Str(String hexStr) { String str = "0123456789ABCDEF"; char[] hexs = hexStr.toCharArray(); byte[] bytes = new byte[hexStr.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } return new String(bytes); }Method 2 of converting strings in hexadecimal strings:
/** * HexString Convert String* @param HexString * @return String */ public static String toStringHex(String s) { byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring( i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "utf-8");// UTF-16le:Not } catch (Exception e1) { e1.printStackTrace(); } return s; }The above article on the conversion of java, the conversion method of Byte and hexadecimal are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.