类型转换类 → ConvertUtils

来源:互联网 发布:大淘客联盟与淘宝联盟 编辑:程序博客网 时间:2024/06/06 01:06
 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()/ 2char[] 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 == 2return String.format("%.02f", value); else if (cnt == 1return String.format("%.01f", value); else return String.format("%.0f", value); }   /** * null转String * @param str * @return */ public static String nullOfString(String str) { if (str == null) { str = ""; } return str; }   /** * String转Byte * @param str * @return */ public static byte stringToByte(String str) { byte b = 0if (str != null) { try { b = Byte.parseByte(str); } catch (Exception e) {   } } return b; }   /** * String转Boolean * @param str * @return */ public static boolean stringToBoolean(String str) { if (str == null) { return false; } elseif (str.equals("1")) { return true; } else if (str.equals("0")) { return false; } elsetryreturn Boolean.parseBoolean(str); } catch (Exception e) { return false; } } } }   /** * String转Int * @param str * @return */ public static int stringToInt(String str) { int i = 0if (str != null) { try { i = Integer.parseInt(str.trim()); } catch (Exception e) { i = 0; }   } else { i = 0; } return i; }   /** * String转Short * @param str * @return */ public static short stringToShort(String str) { short i = 0if (str != null) { try { i = Short.parseShort(str.trim()); } catch (Exception e) { i = 0; } } else { i = 0; } return i; }   /** * String转Double * @param str * @return */ public static double stringToDouble(String str) { double i = 0if (str != null) { try { i = Double.parseDouble(str.trim()); } catch (Exception e) { i = 0; } } else { i = 0; } return i; }   /** * Int转String * @param i * @return */ public static String intToString(int i) { String str = ""try { str = String.valueOf(i); } catch (Exception e) { str = ""; } return str; }   /** * Double转Long * @param d * @return */ public static long doubleToLong(doubled) { long lo = 0try//double转换成long前要过滤掉double类型小数点后数据 lo = Long.parseLong(String.valueOf(d).substring(0,String.valueOf(d).lastIndexOf("."))); } catch (Exception e) { lo = 0; } return lo; }   /** * Double转Int * @param d * @return */ public static int doubleToInt(double d) { int i = 0try//double转换成long前要过滤掉double类型小数点后数据 i = Integer.parseInt(String.valueOf(d).substring(0,String.valueOf(d).lastIndexOf("."))); } catch (Exception e) { i = 0; } return i; }   /** * Long转Double * @param d * @return */ public static double longToDouble(long d) { double lo = 0try { lo = Double.parseDouble(String.valueOf(d)); } catch (Exception e) { lo = 0; } return lo; }   /** * Long转Int * @param d * @return */ public static int longToInt(long d) { int lo = 0try { lo = Integer.parseInt(String.valueOf(d)); } catch (Exception e) { lo = 0; } return lo; }   /** * String转Long * @param str * @return */ public static long stringToLong(String str) { Long li = new Long(0); try { li = Long.valueOf(str); } catch (Exception e) { //li = new Long(0); } return li.longValue(); }   /** * Long转String * @param li * @return */ public static String longToString(long li) { String str = ""try { str = String.valueOf(li); } catch (Exception e) {   } return str; }   }
原创粉丝点击