Java 資料轉換/進位制轉換 工具類
阿新 • • 發佈:2018-11-10
package com.yunduan.parking.util; import java.math.BigInteger; import java.util.regex.Pattern; /** * Created by Administrator on 2018/8/15. */ public class ByteUtil { /** * 十六進位制轉為十進位制 */ public static String getHexToTen(String hex) { return String.valueOf(Integer.parseInt(hex, 16)); } /** * 將十六進位制字串轉為字串型別Ascll碼 */ public static String getHexToAscllString(String hex) { StringBuilder sb = new StringBuilder(); StringBuilder temp = new StringBuilder(); for (int i = 0; i < hex.length() - 1; i += 2) { String output = hex.substring(i, (i + 2)); int decimal = Integer.parseInt(output, 16); sb.append((char) decimal); temp.append(decimal); } return getFileAddSpace(sb.toString()); } /** * 每兩位之間插入空格 */ public static String getFileAddSpace(String replace) { String regex = "(.{2})"; replace = replace.replaceAll(regex, "$1 "); return replace; } /** * 字串轉換為Ascii * * @param value * @return */ public static String stringToAscii(String value) { StringBuffer sbu = new StringBuffer(); char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (i != chars.length - 1) { sbu.append((int) chars[i]).append(","); } else { sbu.append((int) chars[i]); } } return sbu.toString(); } /** * * 十六進位制轉字串 * * * * @param hexString * * 十六進位制字串 * * @param encodeType * * 編碼型別4:Unicode,2:普通編碼 * * @return 字串 * */ public static String hexStringToString(String hexString, int encodeType) { String result = ""; int max = hexString.length() / encodeType; for (int i = 0; i < max; i++) { char c = (char) hexStringToAlgorism(hexString.substring(i * encodeType, (i + 1) * encodeType)); result += c; } return result; } /** * * 十六進位制字串裝十進位制 * * * * @param hex * * 十六進位制字串 * * @return 十進位制數值 * */ public static int hexStringToAlgorism(String hex) { hex = hex.toUpperCase(); int max = hex.length(); int result = 0; for (int i = max; i > 0; i--) { char c = hex.charAt(i - 1); int algorism = 0; if (c >= '0' && c <= '9') { algorism = c - '0'; } else { algorism = c - 55; } result += Math.pow(16, max - i) * algorism; } return result; } /** * 字串轉化成為16進位制字串 */ public static String strTo16(String s) { String str = ""; for (int i = 0; i < s.length(); i++) { int ch = (int) s.charAt(i); String s4 = Integer.toHexString(ch); str = str + s4; } return str; } public static String str2HexStr(String str) { char[] chars = "0123456789ABCDEF".toCharArray(); StringBuilder sb = new StringBuilder(""); byte[] bs = str.getBytes(); int bit; for (int i = 0; i < bs.length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append(chars[bit]); bit = bs[i] & 0x0f; sb.append(chars[bit]); } return sb.toString().trim(); } /** * 16進位制轉換成為string型別字串 * * @param s 十六進位制數 */ public static String hexStringToString(String s) { if (s == null || s.equals("")) { return null; } 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(); } } try { s = new String(baKeyword, "UTF-8"); new String(); } catch (Exception e1) { e1.printStackTrace(); } return s; } /** * 向串列埠傳送資料轉為位元組陣列 * * @param hex 十六進位制數轉byte陣列 */ public static byte[] hex2byte(String hex) { String digital = "0123456789ABCDEF"; String hex1 = hex.replace(" ", ""); char[] hex2char = hex1.toCharArray(); byte[] bytes = new byte[hex1.length() / 2]; byte temp; for (int p = 0; p < bytes.length; p++) { temp = (byte) (digital.indexOf(hex2char[2 * p]) * 16); temp += digital.indexOf(hex2char[2 * p + 1]); bytes[p] = (byte) (temp & 0xff); } return bytes; } /** * 位元組陣列轉16進位制 * * @param bytes 需要轉換的byte陣列 * @return 轉換後的Hex字串 */ public static String bytesToHex(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() < 2) { sb.append(0); } sb.append(hex); } return sb.toString(); } /** * 接收到的位元組陣列轉換16進位制字串 * * @param b 收到的byte陣列 * @param size byte自己陣列長度 * @return 十六進位制字串 */ public static String bytes2HexString(byte[] b, int size) { String ret = ""; for (int i = 0; i < size; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); } return ret; } /** * byte陣列轉成16進位制字串 */ public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * 接收到的位元組陣列轉換16進位制字串 */ public static String byteToStr(byte[] b, int size) { String ret = ""; for (int i = 0; i < size; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); } return ret; } public static int hexStrToTenInteger(String hex) { int bigInteger = Integer.valueOf(new BigInteger(hex, 16).toString()); return bigInteger; } /** * 字串轉byte陣列 */ public static byte[] strToByteArray(String str) { if (str == null) { return null; } byte[] byteArray = str.getBytes(); return byteArray; } /** * 計算CRC16校驗碼 * 逐個求和 * * @param bytes 位元組陣列 * @return {@link String} 校驗碼 * @since 1.0 */ public static String getCRC_16(byte[] bytes) { int CRC = 0x0000ffff; int POLYNOMIAL = 0x0000a001; int i, j; for (i = 0; i < bytes.length; i++) { CRC ^= ((int) bytes[i] & 0x000000ff); for (j = 0; j < 8; j++) { if ((CRC & 0x00000001) != 0) { CRC >>= 1; CRC ^= POLYNOMIAL; } else { CRC >>= 1; } } } if (Integer.toHexString(CRC).toUpperCase().length() == 2) { return "00" + Integer.toHexString(CRC).toUpperCase(); } else if (Integer.toHexString(CRC).toUpperCase().length() == 3) { return "0" + Integer.toHexString(CRC).toUpperCase(); } return Integer.toHexString(CRC).toUpperCase(); } /** * 指令校驗和,並取出後兩位位元組 */ public static String getSum16(byte[] msg, int length) { long mSum = 0; byte[] mByte = new byte[length]; for (byte byteMsg : msg) { long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256); mSum += mNum; } for (int liv_Count = 0; liv_Count < length; liv_Count++) { mByte[length - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff); } return byteToStr(msg, length) + byteToStr(mByte, mByte.length).substring(byteToStr(mByte, mByte.length).length() - 4, byteToStr(mByte, mByte.length).length()); } /** * 高低位轉換 */ public static String getShort(String data) { switch (data.length()) { case 1: data = "000" + data; break; case 2: data = "00" + data; break; case 3: data = "0" + data; break; } String s1 = data.substring(0, 2); String s2 = data.substring(2, 4); return s2 + s1; } //判斷浮點數(double和float) public static boolean isDouble(String str) { if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$"); return pattern.matcher(str).matches(); } /** * 計算校驗值 */ public static String getCRC_16CheckSum(String hexdata) { if (hexdata == null || hexdata.equals("")) { return "00"; } hexdata = hexdata.replaceAll(" ", ""); int total = 0; int len = hexdata.length(); if (len % 2 != 0) { return "00"; } int num = 0; while (num < len) { String s = hexdata.substring(num, num + 2); total += Integer.parseInt(s, 16); num = num + 2; } String data = hexInt(total); return data.substring(data.length() - 2, data.length()); } public static String hexInt(int total) { int a = total / 256; int b = total % 256; if (a > 255) { return hexInt(a) + format(b); } return format(a) + format(b); } public static String format(int hex) { String hexa = Integer.toHexString(hex); int len = hexa.length(); if (len < 2) { hexa = "0" + hexa; } return hexa; } }