在我们进行开发中,会遇到将16进制转换为各种形式,今天我们就详细的讲解一下:
1、十六进制字符串转byte[]
public static byte[] hexStringToBytes(String hexString) {if (hexString == null || hexString.equals("")) {return null;}hexString = hexString.trim();hexString = hexString.toUpperCase();int length = hexString.length() / 2;char[] hexChars = hexString.toCharArray();byte[] d = new byte[length];for (int i = 0; i < length; i++) {int pos = i * 2;d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));}return d; }public static byte charToByte(char c) {return (byte) "0123456789ABCDEF".indexOf(c); }
2、byte[]转十六进制字符串
public static String bytesToHexString(byte[] b) {if (b.length == 0) {return null;}StringBuilder sb = new StringBuilder();for (byte item : b) {int value = item & 0xFF;String hv = Integer.toHexString(value);if (hv.length() < 2) {sb.append(0);}sb.append(hv);}return sb.toString(); }
3、十六进制字符串 高低位转换
public static String reverseHex(String hex) {char[] charArray = hex.toCharArray();int length = charArray.length;int times = length / 2;for (int c1i = 0; c1i < times; c1i += 2) {int c2i = c1i + 1;char c1 = charArray[c1i];char c2 = charArray[c2i];int c3i = length - c1i - 2;int c4i = length - c1i - 1;charArray[c1i] = charArray[c3i];charArray[c2i] = charArray[c4i];charArray[c3i] = c1;charArray[c4i] = c2;}return new String(charArray); }
4、十六进制字符串转换为float符点型
public static long parseLong(String s, int radix) throws NumberFormatException {if (s == null) {throw new NumberFormatException("null");}if (radix < Character.MIN_RADIX) {throw new NumberFormatException("radix" + radix + "less than Character.MIN_RADIX");}if (radix > Character.MAX_RADIX) {throw new NumberFormatException("radix" + radix + "greater than Character.MAX_RADIX");}long result = 0;boolean negative = false;int i = 0, len = s.length();long limit = -Long.MAX_VALUE;long multmin;int digit;if (len > 0) {char firstChar = s.charAt(0);if (firstChar < '0') {// Possible leading "+" or "-"if (firstChar == '-') {negative = true;limit = Long.MIN_VALUE;} else if (firstChar != '+')throw NumberFormatException.forlnputString(s);if (len == 1) // Cannot have lone "+" or "-"throw NumberFormatException.forlnputString(s);i++;}multmin = limit / radix;while (i < len) {// Accumulating negatively avoids surprises near MAX_VALUEdigit = Character.digit(s.charAt(i++), radix);if (digit < 0) {throw NumberFormatException.forlnputString(s);}if (result < multmin) {throw NumberFormatException.forlnputString(s);}result *= radix;if (result < limit + digit) {throw NumberFormatException.forlnputString(s);}result -= digit;}} else {throw NumberFormatException.forlnputString(s);}return negative ? result : -result; }/*** NuberFormatException*/ public static class NumberFormatException extends IllegalAccessException {/****/private static final long serialVersionUID = 1L;public NumberFormatException(String s) {super(s);}static NumberFormatException forlnputString(String s) {return new NumberFormatException("For input string: \"" + s + "\"");} }
以上就是讲解的主要的转换方式,以供大家参考。