Skip to content

Instantly share code, notes, and snippets.

@guozi
Forked from keepingcoding/HexUtils.java
Created November 17, 2021 09:25
Show Gist options
  • Save guozi/80cc74b2525f7a3e92133e1aadf9c936 to your computer and use it in GitHub Desktop.
Save guozi/80cc74b2525f7a3e92133e1aadf9c936 to your computer and use it in GitHub Desktop.
进制转换工具类
public class HexUtils {
private HexUtils() {
}
/**
* 16进制转2进制
*
* @param str
* @return
*/
public static byte[] hexStrToByte(String str) {
int length = str.length() / 2;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = (byte)((Character.digit(str.charAt(i * 2), 16) << 4) |
Character.digit(str.charAt((i * 2) + 1), 16));
}
return bytes;
}
/**
* 2进制转16进制
*
* @param bytes
* @return
*/
public static String byteToHexStr(byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length << 1);
for (byte b : bytes) {
buf.append(String.format("%02x", new Integer(b & 0xff)));
}
return buf.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment