package util; /** * Java中16进制bety[](数组)与String字符串相互转换 * @author qiheo.com */ public class Util { /** * 16进制bety[]转换String字符串.方法一 * @param data * @return String 返回字符串无空格 */ public static String bytesToString(byte[] data) { char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[data.length * 2]; for (int j = 0; j < data.length; j++) { int v = data[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } String result = new String(hexChars); return result; } /** * 16进制bety[]转换String字符串.方法二 * @param data * @return String 返回字符串有空格 */ public static String bytesToString2(byte[] bytes) { final char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[bytes.length * 2]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xFF; hexChars[i * 2] = hexArray[v >>> 4]; hexChars[i * 2 + 1] = hexArray[v & 0x0F]; sb.append(hexChars[i * 2]); sb.append(hexChars[i * 2 + 1]); sb.append(' '); } return sb.toString(); } /** * 16进制bety[]转换String字符串.方法三 * @param data * @return String 返回字符串有空格 */ private static String bytesToString3(byte[] arg) { String result = new String(); if (arg != null) { for (int i = 0; i < arg.length; i++) { result = result + (Integer.toHexString( arg[i] < 0 ? arg[i] + 256 : arg[i]).length() == 1 ? "0" + Integer.toHexString(arg[i] < 0 ? arg[i] + 256 : arg[i]) : Integer.toHexString(arg[i] < 0 ? arg[i] + 256 : arg[i])) + " "; } return result; } return ""; } /** * String字符串转换16进制bety[].方法一 * @param data * @return byte[] */ public static byte[] stringToBytes(String s) { s = s.replace(" ", ""); s = s.replace("#", ""); byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } return baKeyword; } /** * String字符串转换16进制bety[].方法二 * @param data * @return byte[] */ private static byte[] stringToBytes2(String arg) { if (arg != null) { char[] NewArray = new char[1000]; char[] array = arg.toCharArray(); int length = 0; for (int i = 0; i < array.length; i++) { if (array[i] != ' ') { NewArray[length] = array[i]; length++; } } int EvenLength = (length % 2 == 0) ? length : length + 1; if (EvenLength != 0) { int[] data = new int[EvenLength]; data[EvenLength - 1] = 0; for (int i = 0; i < length; i++) { if (NewArray[i] >= '0' && NewArray[i] <= '9') { data[i] = NewArray[i] - '0'; } else if (NewArray[i] >= 'a' && NewArray[i] <= 'f') { data[i] = NewArray[i] - 'a' + 10; } else if (NewArray[i] >= 'A' && NewArray[i] <= 'F') { data[i] = NewArray[i] - 'A' + 10; } } byte[] byteArray = new byte[EvenLength / 2]; for (int i = 0; i < EvenLength / 2; i++) { byteArray[i] = (byte) (data[i * 2] * 16 + data[i * 2 + 1]); } return byteArray; } } return new byte[] {}; } /** * String字符串转换16进制bety[].方法三 * @param data * @return byte[] */ public static byte[] stringToBytes3(String str) { if(str == null || str.trim().equals("")) { return new byte[0]; } str = str.replace(" ", ""); 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; } /** * String字符串转换16进制bety[].方法四 * @param data * @return byte[] */ public static byte[] stringToBytes4(String hex) { hex = hex.replace(" ", ""); if ((hex == null) || (hex.equals(""))){ return null; } else if (hex.length()%2 != 0){ return null; } else{ hex = hex.toUpperCase(); int len = hex.length()/2; byte[] b = new byte[len]; char[] hc = hex.toCharArray(); for (int i=0; i<len; i++){ int p=2*i; b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1])); } return b; } } /** * char进制转换 * @param char * @return byte */ private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } }
本文由一叶发布,不代表一叶立场,转载联系作者并注明出处:https://yiyeo.com/java/25.html