-
-
Save guozi/80cc74b2525f7a3e92133e1aadf9c936 to your computer and use it in GitHub Desktop.
进制转换工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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