1/** 2 * 数据类型转换工具类 3 * @author cyf 4 * 5 */ 6public class NumConvertUtil{ 7 8 9 /** 10 * bytes 转16进制字符串 11 * @param bArray 12 * @return 13 */ 14 public static final String bytesToHexString(byte[] bArray) { 15 StringBuffer sb = new StringBuffer(bArray.length); 16 String sTemp; 17 for (int i = 0; i < bArray.length; i++) { 18 sTemp = Integer.toHexString(0xFF & bArray[i]); 19 if (sTemp.length() < 2) 20 sb.append(0); 21 sb.append(sTemp.toUpperCase()); 22 } 23 return sb.toString(); 24 } 25 26 27 28 29 /** 30 * 16进制字符串转bytes 31 * @param hex 32 * @return 33 */ 34 public static byte[] hexStringToByte(String hex) { 35 36 int len = 0; 37 int num=0; 38 39 //判断字符串的长度是否是两位 40 if(hex.length()>=2){ 41 42 //判断字符喜欢是否是偶数 43 len=(hex.length() / 2); 44 num = (hex.length() % 2); 45 46 if (num == 1) { 47 hex = "0" + hex; 48 len=len+1; 49 } 50 51 52 }else{ 53 54 hex = "0" + hex; 55 len=1; 56 57 58 } 59 60 61 byte[] result = new byte[len]; 62 char[] achar = hex.toCharArray(); 63 for (int i = 0; i < len; i++) { 64 int pos = i * 2; 65 result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); 66 } 67 return result; 68 69 70 71 } 72 73 74 private static int toByte(char c) { 75 76 77 if (c >= 'a') 78 return (c - 'a' + 10) & 0x0f; 79 if (c >= 'A') 80 return (c - 'A' + 10) & 0x0f; 81 return (c - '0') & 0x0f; 82 83 84 } 85 86 87 /** 88 * 16进制字符串转十进制int 89 * @param HexString 90 * @return 91 */ 92 public static int HexStringToInt(String HexString) { 93 94 int inJTFingerLockAddress = Integer.valueOf(HexString, 16); 95 96 return inJTFingerLockAddress; 97 } 98 99 100 101 102 /** 103 * 十进制int转16进制字符串 104 * @param HexString 105 * @return 106 */ 107 public static String IntToHexString(int num) { 108 109 String hexString = Integer.toHexString(num); 110 111 return hexString; 112 } 113 114 115 /** 116 * 16进制String转BCD 117 * @param asc 118 * @return 119 */ 120 public static byte[] strToBcd(String asc) { 121 int len = asc.length(); 122 int mod = len % 2; 123 124 if (mod != 0) { 125 asc = "0" + asc; 126 len = asc.length(); 127 } 128 129 byte abt[] = new byte[len]; 130 if (len >= 2) { 131 len = len / 2; 132 } 133 134 byte bbt[] = new byte[len]; 135 abt = asc.getBytes(); 136 int j, k; 137 138 for (int p = 0; p < asc.length()/2; p++) { 139 if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { 140 j = abt[2 * p] - '0'; 141 } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { 142 j = abt[2 * p] - 'a' + 0x0a; 143 } else { 144 j = abt[2 * p] - 'A' + 0x0a; 145 } 146 147 if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { 148 k = abt[2 * p + 1] - '0'; 149 } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { 150 k = abt[2 * p + 1] - 'a' + 0x0a; 151 }else { 152 k = abt[2 * p + 1] - 'A' + 0x0a; 153 } 154 155 int a = (j << 4) + k; 156 byte b = (byte) a; 157 bbt[p] = b; 158 } 159 return bbt; 160 } 161 162 163 /** 164 * String 类型数字转化为Double 保留置顶小数位(用于显示金额等。) 165 * @param money 166 * @param type 保留小数点位数 #.00保留两位 #.0保留一位 #保留整数 167 * @return 168 */ 169 public static String strToDouble(String money,String type){ 170 171 String toDouble= new DecimalFormat(type).format(Double.parseDouble(money)); 172 173 return toDouble; 174 175 } 176 177 178 179 180 181 }
十六进制字符串高低位转换(“dc45fd45”转化成“45fd45dc”)
1private static String lockAddress(String lockAddress) { 2 3 StringBuffer s1 = new StringBuffer(lockAddress); 4 int index; 5 for (index = 2; index < s1.length(); index += 3) { 6 s1.insert(index, ','); 7 } 8 String[] array = s1.toString().split(","); 9 String[] swapOrder = swapOrder(array); 10 StringBuffer s2 = new StringBuffer(); 11 for (String string :swapOrder ) { 12 s2.append(string); 13 } 14 return s2.toString(); 15 16 } 17 18 19 public static String[] swapOrder(String[] arr){ 20 int length = arr.length; 21 for(int i=0;i<length/2;i++){ //只需一个循环,数组的一半就可以,第一个和最后一个交换,第二个和倒数第二个交换。。。 22 String temp = arr[i]; 23 arr[i] = arr[length-1-i]; 24 arr[length-1-i] = temp; 25 } 26 return arr; 27 } 28 29 30 31 public static void main(String[] args) throws Exception { 32 33 34 System.out.println(ServerDoor.lockAddress("dc45fd45")); 35 //输出结果:45fd45dc 36 37 }
记录备忘,以后会补充,有错误请大神指正。