package com.jingewenku.abrahamcaijin.commonutil; /** * @Description:主要功能: 类型转换类 * @Prject: CommonUtilLibrary * @Package: com.jingewenku.abrahamcaijin.commonutil * @author: AbrahamCaiJin * @date: 2017年05月16日 15:26 * @Copyright: 个人版权所有 * @Company: * @version: 1.0.0 */ public class ConvertUtils { private ConvertUtils() { throw new UnsupportedOperationException("cannot be instantiated"); } /** * 十六进制字符串转换为byte数组 * * @param hexString * @return */ public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } 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; } /** * char转换为byte数组 * @param c * @return */ public static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /** * byte数组转换为十六进制字符串 * * @param b * @return */ public static String bytesToHexString(byte[] b) { if (b.length == 0) { return null; } StringBuilder sb = new StringBuilder(""); for (int i = 0; i < b.length; i++) { int value = b[i] & 0xFF; String hv = Integer.toHexString(value); if (hv.length() < 2) { sb.append(0); } sb.append(hv); } return sb.toString(); } /** * int转换为byte数组 * * @param res * @return */ public static byte[] intToByte(int res) { byte[] targets = new byte[4]; targets[0] = (byte) (res & 0xff);// 最低位 targets[1] = (byte) ((res >> 8) & 0xff);// 次低位 targets[2] = (byte) ((res >> 16) & 0xff);// 次高位 targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。 return targets; } /** * byte数组转换为int * * @param res * @return */ public static int byteToInt(byte[] res) { // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000 int targets = (res[3] & 0xff) | ((res[2] << 8) & 0xff00) | ((res[1] << 16) & 0xff0000) | ((res[0] << 24) & 0xff000000); return targets; } /** * 保留几位小数 */ public static String saveDecimals(int cnt, double value) { if (cnt == 2) return String.format("%.02f", value); else if (cnt == 1) return String.format("%.01f", value); else return String.format("%.0f", value); } }