Java基本数据的位运算

  • byte数组转16进制字符串:
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();  
public static String bytesToHex(byte[] bytes) {  
    char[] hexChars = new char[bytes.length * 2];  
    for ( int j = 0; j < bytes.length; j++ ) {  
        int v = bytes[j] & 0xFF;  
        hexChars[j * 2] = hexArray[v >>> 4];  
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];  
    }  
    return new String(hexChars);  
} 
  • 16进制字符串转byte数组
public static byte[] toBytes(String str) { 
    if (str == null || str.trim().equals("")) { 
        return new byte[0];
    }
    byte[] bytes = new byte[str.length() / 2];
    for (int i = 0; i < str.length() / 2; i++) {
         String subStr = str.substring(i * 2, i * 2 + 2);
         bytes[i] = (byte) Integer.parseInt(subStr, 16);
    }
    return bytes;
}

你可能感兴趣的:(Java基本数据的位运算)