Preface
When Java communicates with programs written in other languages in tcp/ip socket, the communication content is generally converted into byte array type, and Java is also very convenient to convert characters and arrays. Let me learn about the principle of converting strings and byte
principle
We all know that in Java, the byte type takes 1 byte, that is, 8 bits, while the hexadecimal characters occupy 4 bits, so each byte can be represented by two characters, and vice versa.
Give an example
byte = 123
Expressed in binary: 0111 1011
Each 4 digits are represented by characters: 7 b
Yes, the principle is that simple, let's implement it with code:
byte[] to hexadecimal string
Method 1
Idea: first convert byte[] to dimensional char[] , and then convert char[] to string
public static String bytes2Hex(byte[] src) { if (src == null || src.length <= 0) { return null; } char[] res = new char[src.length * 2]; // Each byte corresponds to two characters final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; for (int i = 0, j = 0; i < src.length; i++) { res[j++] = hexDigits[src[i] >> 4 & 0x0f]; // Save the high 4 bits of byte first res[j++] = hexDigits[src[i] & 0x0f]; // Save the low 4 bits of byte again} return new String(res); } Method 2
Idea: first convert byte to int type, then convert to string
public static String bytesToHex(byte[] src){ if (src == null || src.length <= 0) { return null; } StringBuilder stringBuilder = new StringBuilder(""); for (int i = 0; i < src.length; i++) { // The reason for using byte and 0xff is to be combined is because int is 32 bits. After matching with 0xff, the first 24 bits are discarded, and only the last 8 bits are retained String str = Integer.toHexString(src[i] & 0xff); if (str.length() < 2) { // If less than two bits are needed, 0 must be added stringBuilder.append(0); } stringBuilder.append(str); } return stringBuilder.toString(); } Hexadecimal string to byte[]
Idea: first convert the string to char[] , then convert it to byte[]
public static byte[] hexToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] bytes = new byte[length]; String hexDigits = "0123456789abcdef"; for (int i = 0; i < length; i++) { int pos = i * 2; // Two characters correspond to one byte int h = hexDigits.indexOf(hexChars[pos]) << 4; // Note 1 int l = hexDigits.indexOf(hexChars[pos + 1]); // Note 2 if(h == -1 || l == -1) { // Non-hexadecimal characters return null; } bytes[i] = (byte) (h | l); } return bytes; } Note: Note 1 gets xxxx0000 , Note 2 gets 0000xxxx , and then converts two characters into a byte .
Give another example
md5 encryption
public static String getMd5ByFile(File file) { String ret= null; FileInputStream fis = null; try { fis = new FileInputStream(file); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) > 0) { md.update(buffer, 0, len); } ret = bytes2Hex(md.digest()); // Convert md5 encrypted byte[] into a string} catch (Exception e) { e.printStackTrace(); } finally { if(fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return return; }Summarize
OK, I should understand, it's not difficult. The above is the entire content of this article. I hope the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.