Bootstrap

Java将byte数组转为Hex16进制字符串

Java将byte数组转为Hex16进制字符串

将digest的结果数组转为16进制

假设已得到结果: resultByteArray

将其转为16进制的办法

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02x", bytes[i]));
        }
        return sb.toString();
    }
    String resultHex = bytesToHex(resultByteArray)

小写

public static String hexLowerCaseFromByteArray(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)>>>4)).append(simple.charAt(b<<28>>>28));
		}
		return sb.toString();
	}
	String resultHex = bytesToHex(resultByteArray)

大写

	public static String hexUpperCaseFromByteArray(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)>>>4)).append(simple.charAt(b<<28>>>28));
		}
		return sb.toString();
	}
	String resultHex = bytesToHex(resultByteArray)
	public static String hexLowerCaseFromByteArray2(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)/16)).append(simple.charAt((b&0xff)%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray2(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)/16)).append(simple.charAt((b&0xff)%16));
		}
		return sb.toString();
	}
	
	public static String hexLowerCaseFromByteArray3(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&255)/16)).append(simple.charAt((b&255)%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray3(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&255)/16)).append(simple.charAt((b&255)%16));
		}
		return sb.toString();
	}
	
	
	public static String hexLowerCaseFromByteArray4(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			int i=b>=0?b:b+256;
			sb.append(simple.charAt(i/16)).append(simple.charAt(i%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray4(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			int i=b>=0?b:b+256;
			sb.append(simple.charAt(i/16)).append(simple.charAt(i%16));
		}
		return sb.toString();
	}
	
	public static String hexLowerCaseFromByteArray5(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt(Byte.toUnsignedInt(b)/16)).append(simple.charAt(Byte.toUnsignedInt(b)%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray5(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt(Byte.toUnsignedInt(b)/16)).append(simple.charAt(Byte.toUnsignedInt(b)%16));
		}
		return sb.toString();
	}

测试

packagebyte数组转为16进制字符串的方法;

import java.awt.FileDialog;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.security.MessageDigest;

import javax.swing.JFrame;

public class ByteArrayToHexString {
	
	public static String hexLowerCaseFromByteArray(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)>>>4)).append(simple.charAt(b<<28>>>28));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)>>>4)).append(simple.charAt(b<<28>>>28));
		}
		return sb.toString();
	}
	
	
	public static String hexLowerCaseFromByteArray2(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)/16)).append(simple.charAt((b&0xff)%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray2(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&0xff)/16)).append(simple.charAt((b&0xff)%16));
		}
		return sb.toString();
	}
	
	
	
	public static String hexLowerCaseFromByteArray3(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&255)/16)).append(simple.charAt((b&255)%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray3(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt((b&255)/16)).append(simple.charAt((b&255)%16));
		}
		return sb.toString();
	}
	
	
	public static String hexLowerCaseFromByteArray4(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			int i=b>=0?b:b+256;
			sb.append(simple.charAt(i/16)).append(simple.charAt(i%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray4(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			int i=b>=0?b:b+256;
			sb.append(simple.charAt(i/16)).append(simple.charAt(i%16));
		}
		return sb.toString();
	}
	
	
	public static String hexLowerCaseFromByteArray5(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789abcdef";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt(Byte.toUnsignedInt(b)/16)).append(simple.charAt(Byte.toUnsignedInt(b)%16));
		}
		return sb.toString();
	}
	
	public static String hexUpperCaseFromByteArray5(byte[] byteArray) {
		if(byteArray==null)return null;
		String simple = "0123456789ABCDEF";
		StringBuilder sb = new StringBuilder(byteArray.length*2);
		for(byte b : byteArray) {
			sb.append(simple.charAt(Byte.toUnsignedInt(b)/16)).append(simple.charAt(Byte.toUnsignedInt(b)%16));
		}
		return sb.toString();
	}
	
	public static String hexLowerCaseFromByteArray6(byte[] byteArray) {
		if(byteArray==null)return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            sb.append(String.format("%02x", byteArray[i]));
        }
        return sb.toString();
	}
	
	
	public static void main(String...arguments)throws Exception{
		JFrame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());
		frame.addWindowListener(new WindowAdapter() {
			@Override public void windowClosing(WindowEvent ev) {System.exit(0);}
		});
		FileDialog fDialog = new FileDialog(frame);
		
		fDialog.setVisible(true);
		
		if(fDialog.getFile()!=null) {
			String pathFile = fDialog.getDirectory()+fDialog.getFile();
			FileInputStream is = new FileInputStream(pathFile);
			MessageDigest digest = MessageDigest.getInstance("SHA-256");
			
			byte[] bf = new byte[10240];
			int numRead;
			do {
			    numRead = is.read(bf);
			    if (numRead > 0) {
			        digest.update(bf, 0, numRead);
			    }
			} while (numRead != -1);
			
			is.close();

			byte[] resultByteArray = digest.digest();
			
			String result;
			
			result = hexLowerCaseFromByteArray(resultByteArray); System.out.println(result);
			result = hexUpperCaseFromByteArray(resultByteArray); System.out.println(result);
			result = hexLowerCaseFromByteArray2(resultByteArray); System.out.println(result);
			result = hexUpperCaseFromByteArray2(resultByteArray); System.out.println(result);
			result = hexLowerCaseFromByteArray3(resultByteArray); System.out.println(result);
			result = hexUpperCaseFromByteArray3(resultByteArray); System.out.println(result);
			result = hexLowerCaseFromByteArray4(resultByteArray); System.out.println(result);
			result = hexUpperCaseFromByteArray4(resultByteArray); System.out.println(result);
			result = hexLowerCaseFromByteArray5(resultByteArray); System.out.println(result);
			result = hexUpperCaseFromByteArray5(resultByteArray); System.out.println(result);
			result = hexLowerCaseFromByteArray6(resultByteArray); System.out.println(result);
		}
		
		System.exit(0);
	}
	

}

;