类型转换类 → ConvertUtils

 
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);
  }
   
  /**
  * 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 = 0;
  if (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;
  } else {
  if (str.equals("1")) {
  return true;
  } else if (str.equals("0")) {
  return false;
  } else {
  try {
  return Boolean.parseBoolean(str);
  } catch (Exception e) {
  return false;
  }
  }
  }
  }
   
  /**
  * String转Int
  * @param str
  * @return
  */
  public static int stringToInt(String str) {
  int i = 0;
  if (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 = 0;
  if (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 = 0;
  if (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(double d) {
  long lo = 0;
  try {
  //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 = 0;
  try {
  //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 = 0;
  try {
  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 = 0;
  try {
  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;
  }
   
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值