字节数组转成16进制
The simple waypublic static String getHexString(byte[] b) throws Exception { String result = ""; for (int i=0; i < b.length; i++) { result += String( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); } return result; } A faster wayimport java.io.UnsupportedEncodingException; public class StringUtils { static final byte[] HEX_CHAR_TABLE = { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' }; public static String getHexString(byte[] raw) throws UnsupportedEncodingException { byte[] hex = new byte[2 * raw.length]; int index = 0; for (byte b : raw) { int v = b & 0xFF; hex[index++] = HEX_CHAR_TABLE[v >>> 4]; hex[index++] = HEX_CHAR_TABLE[v & 0xF]; } return new String(hex, "ASCII"); } public static void main(String args[]) throws Exception{ byte[] byteArray = { (byte)255, (byte)254, (byte)253, (byte)252, (byte)251, (byte)250 }; System.out.HexString(byteArray)); /* * output : * fffefdfcfbfa */ } } A more elegant (based on a suggestion by Lew on usenet-cljp) static final String HEXES = "0123456789ABCDEF"; public static String getHex( byte [] raw ) { if ( raw == null ) { return null; } final StringBuilder hex = new StringBuilder( 2 * raw.length ); for ( final byte b : raw ) { hex.append(HEXES.charAt((b & 0xF0) >> 4)) .append(HEXES.charAt((b & 0x0F))); } String(); }16进制字符串转16进制数组

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。