1. Byte to decimal
Use the (int) type conversion directly.
/* * Byte to decimal*/ public static int byte2Int(byte b){ int r = (int) b; return r; }2. Decimal to bytes
Directly use (byte) type conversion.
/* * decimal to byte*/ public static byte int2Byte(int i){ byte r = (byte) i; return r; }3. Byte array to hexadecimal string
For each byte, first do the same operation with 0xFF, and then use the Integer.toHexString() function. If the result is only 1 bit, you need to add 0 in front.
/* * Byte array to hex string*/ public static String bytes2HexString(byte[] b) { String r = ""; for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } r += hex.toUpperCase(); } return r; }4. Hexadecimal string to byte array
This is more complicated. Each hexadecimal character is 4 bits and one byte is 8 bits, so two hexadecimal characters are converted into 1 byte. For the first character, convert it into byte and then shift it left by 4 bits, and then do or operate with the second character byte, so that the two characters are converted into 1 byte.
/* * Characters are converted to byte*/ private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /* * Hex string to byte array*/ public static byte[] hexString2Bytes(String hex) { if ((hex == null) || (hex.equals(""))){ return null; } else if (hex.length()%2 != 0){ return null; } else{ hex = hex.toUpperCase(); int len = hex.length()/2; byte[] b = new byte[len]; char[] hc = hex.toCharArray(); for (int i=0; i<len; i++){ int p=2*i; b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1])); } return b; } }5. To convert a byte array into a string
Use new String() directly.
/* * Byte array to string*/ public static String bytes2String(byte[] b) throws Exception { String r = new String (b,"UTF-8"); return r; }6. String to byte array
Use getBytes() directly.
/* * String to byte array*/ public static byte[] string2Bytes(String s){ byte[] r = s.getBytes(); return r; }7. Hexadecimal string to string
Convert to byte[] first, then convert to string.
/* * HexString to String*/ public static String hex2String(String hex) throws Exception{ String r = bytes2String(hexString2Bytes(hex)); return r; }8. String to hexadecimal string
Convert to byte[] first, then convert to hexadecimal string.
/* * String to hexadecimal string*/ public static String string2HexString(String s) throws Exception{ String r = bytes2HexString(string2Bytes(s)); return r; }Main function:
public static void main(String[] args) throws Exception{ byte b1 = (byte) 45; System.out.println("1. Byte to decimal:" + byte2Int(b1)); int i = 89; System.out.println("2.1 to byte to byte:" + int2Byte(i)); byte[] b2 = new byte[]{(byte)0xFF, (byte)0x5F, (byte)0x6, (byte)0x5A}; System.out.println("3. Byte array to hexadecimal string:" + bytes2HexString(b2)); String s1 = new String("1DA47C"); System.out.println("4.16% string to byte array:" + Arrays.toString(hexString2Bytes(s1))); System.out.println("5. Byte array to string:" + bytes2String(b2)); System.out.println("6. String to byte array:" + Arrays.toString(string2Bytes(s1))); System.out.println("7.16% string to string:" + hex2String(s1)); String s2 = new String("Hello!"); System.out.println("8. string to hexadecimal string:" + string2HexString(s2)); }Running results:
1. Byte to decimal: 452.1 decimal to byte: 893. Byte array to hexadecimal string: FF5F065A4. Hexadecimal string to byte array: [29, -92, 124]5. Byte array to string:?_ Z6. String to byte array: [49, 68, 65, 52, 55, 67]7. Hexadecimal string to string: ?|8. String to hexadecimal string: 48656C6C6F21
The above article briefly discusses the mutual conversion between binary, decimal, hexadecimal and strings is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.