java代码,2进制转16进制,或者16进制转2进制

java代码,2进制转16进制,或者16进制转2进制

 

private static byte[] hex2Bin(String src) { if (src.length() < 1) return null; byte[] encrypted = new byte[src.length() / 2]; for (int i = 0; i < src.length() / 2; i++) { int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16); encrypted[i] = (byte) (high * 16 + low); } return encrypted; } private static String byte2hexString(byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for( i=0;i<buf.length;i++){ strbuf.append( Integer.toString((buf[i]>>4) & 0xf, 16 )+ Integer.toString( buf[i] & 0xf, 16 )); } //同上 // for (i = 0; i < buf.length; i++) { // if (((int) buf[i] & 0xff) < 0x10) // strbuf.append("0"); // // strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); // } return strbuf.toString(); }

你可能感兴趣的:(java代码,2进制转16进制,或者16进制转2进制)