常用类型转换方法工具类

此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

功能:十六进制字符串与字节数组互转;字符转字节;Blob类型转字节数组;阿拉伯数字转中文小写;

  利用反射机制,将源对象转为目标对象;

类型转换类:ConvertUtil.java

  1 package com.ims.common;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.IOException;
  5 import java.lang.reflect.Method;
  6 import java.sql.Blob;
  7 
  8 /**
  9  * 常用类型转换方法工具类
 10  */
 11 public class ConvertUtil {
 12     
 13    /**
 14     *  字节数组转换为十六进制字符串.
 15     *  @param src 
 16     *  @return 十六进制字符串
 17     */   
 18    public static String bytesToHexString(byte[] src){
 19        StringBuilder stringBuilder = new StringBuilder("");
 20        if (src == null || src.length <= 0) {
 21            return null;
 22        }
 23        for (int i = 0; i < src.length; i++) {
 24            int v = src[i] & 0xFF;
 25            String hv = Integer.toHexString(v);
 26            if (hv.length() < 2) {
 27                stringBuilder.append(0);
 28            }
 29            stringBuilder.append(hv);
 30        }
 31        return stringBuilder.toString();
 32    }
 33    
 34    /**
 35     *  十六进制字符串转换为字节数组
 36     *  @param hexString 十六进制字符串
 37     *  @return 
 38     */
 39    public static byte[] hexStringToBytes(String hexString) {
 40        if (hexString == null || hexString.equals("")) {
 41            return null;
 42        }
 43        hexString = hexString.toUpperCase();
 44        int length = hexString.length() / 2;
 45        char[] hexChars = hexString.toCharArray();
 46        byte[] d = new byte[length];
 47        for (int i = 0; i < length; i++) {
 48            int pos = i * 2;
 49            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
 50        }
 51        return d;
 52    }
 53    
 54    /**
 55     * 字符转换为字节
 56     * @param c 字符
 57     * @return
 58     */
 59    private static byte charToByte(char c) {
 60        return (byte) "0123456789ABCDEF".indexOf(c);
 61    }
 62    
 63    /**
 64     * Blob类型转换为字节数组
 65     * @param blob
 66     * @return
 67     */
 68     public static byte[] blobToBytes(Blob blob) {
 69         BufferedInputStream is = null;
 70         try {
 71             is = new BufferedInputStream(blob.getBinaryStream());
 72             byte[] bytes = new byte[(int) blob.length()];
 73             int len = bytes.length;
 74             int offset = 0;
 75             int read = 0;
 76     
 77             while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
 78                 offset += read;
 79             }
 80             return bytes;
 81         } catch (Exception e) {
 82             return null;
 83         } finally {
 84             try {
 85                 is.close();
 86                 is = null;
 87             } catch (IOException e) {
 88                 return null;
 89             }
 90         }
 91     }
 92     
 93     /**
 94      * 阿拉伯数字转中文小写
 95      * @param si 阿拉伯数字
 96      * @return 中文小写字符串
 97      */
 98     public static String transition(String si){  
 99         String []aa={"","十","百","千","万","十万","百万","千万","亿","十亿"};  
100         String []bb={"一","二","三","四","五","六","七","八","九"};  
101         char[] ch=si.toCharArray();  
102         int maxindex=ch.length;
103         // 字符的转换
104         String result = "";
105         // 两位数的特殊转换  
106         if(maxindex==2){   
107             for(int i=maxindex-1,j=0;i>=0;i--,j++){    
108                 if(ch[j]!=48){     
109                     if(j==0&&ch[j]==49){      
110                         result += aa[i];     
111                     }else{      
112                         result += bb[ch[j]-49]+aa[i];     
113                     }    
114                 }   
115             }
116         // 其他位数的特殊转换,使用的是int类型最大的位数为十亿  
117         }else{   
118             for(int i=maxindex-1,j=0;i>=0;i--,j++){    
119                 if(ch[j]!=48){     
120                     result += bb[ch[j]-49]+aa[i];    
121                 }   
122             }  
123         }
124         
125         return result;
126     } 
127     
128     /**
129      * 利用反射机制,将源对象转换为目标对象,属性名一致,且源对象有get方法,目标对象有set方法
130      * @param src 源对象
131      * @param target 目标对象
132      * @return 赋值后的目标对象
133      */
134     public static Object autoGetterAndSetter(Object src, Object target) {
135         Method[] sms = src.getClass().getMethods();     // 原始类方法srcMethod
136         Method[] tms = target.getClass().getMethods();     // 目标类方法targetMethod
137         for (Method sm : sms) {
138             if (sm.getName().startsWith("get")) {        // 原始类的 getter
139                 String attrName = sm.getName().substring(3);// 属性
140                 for (Method tm : tms) {// 遍历目标方法
141                     if (("set" + attrName).equals(tm.getName())) {// 执行目标类的指定attrName的setter
142                         try{
143                             if(!(null==sm.invoke(src))){                                
144                                 tm.invoke(target, sm.invoke(src));                                
145                             }        
146                         }catch(Exception e){
147                             continue;
148                         }
149                     }
150                 }
151             }
152         }
153         return target;
154     }
155 }
View Code
原文地址:https://www.cnblogs.com/Mr-kevin/p/5560490.html