

package fr.zng.xxzx.common.util;

import java.math.BigDecimal;
import java.nio.charset.Charset;

public class ConvertUtil {

	 public static void main(String[] arg) {

	     System.out.println(StringUtil.padLeft( Integer.toHexString((new BigDecimal("88.88")).multiply(new BigDecimal(100))
             .setScale(0, BigDecimal.ROUND_UP).intValue()), 8, '0'));
	    }

	 /**
	     * 转码
	     * @param data
	     * @return
	     */
	  public static String asciiToHex(String data) {
	        
	        byte[] byteArr = data.getBytes(Charset.forName("GBK"));
	        int[] inArr = new int[byteArr.length];
	        String hex = "";
	        for (int i =0; i < byteArr.length; i++) {
	            inArr[i] =  byteArr[i] & 0xff;
	            hex=hex + Integer.toHexString(byteArr[i] & 0xff);
	        }
	        
	        return hex.toUpperCase();
	    }
	 public static String convertStringToHex2(String str){

	      char[] chars = str.toCharArray();

	      StringBuffer hex = new StringBuffer();
	      for(int i = 0; i < chars.length; i++){
	        hex.append(Integer.toHexString((int)chars[i]));
	      }

	      return hex.toString();
	      }

	
	public static String toFZDA(String data) {
		if (StringUtil.isEmpty(data) || (data.length() % 2 != 0)) {
			return "";
		}
		int i =0;
		String ret = "";
		for (i =data.length(); i >0; i=i-2 ) {
			ret = ret + data.substring(i-2, i);
		}
		return ret;
	}

	
	public static String tpCardMoney(String data) {
        if (StringUtil.isEmpty(data) || (data.length() % 2 != 0)) {
            return "";
        }
        int i =0;
        String ret = "";
        for (i =data.length(); i >0; i=i-2 ) {
            ret = ret + data.substring(i-2, i);
        }
        
        
        return ret;
    }
	
	public static String toStrMoney(String data) {
	    //
        if (StringUtil.isEmpty(data)) {
          return "0000.00";  
        }
        int in = data.indexOf(".");
       
        if (in < 0) {
            return StringUtil.padLeft(data, 4, '0') + ".00";
        } else {
            int sl = data.substring(in + 1).length();
            if (sl == 1) {
                return StringUtil.padLeft(data, 6, '0') + "0";
            } else {
                return StringUtil.padLeft(data, 7, '0');
            }
        }
    }

    /**
     * 转化十六进制编码为字符串
     * @param s
     * @return
     */
    public static String toStringHex1(String s) {
        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");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
    
    /** 
     * @Title:string2HexString 
     * @Description:字符串转16进制字符串 
     * @param strPart 
     *            字符串 
     * @return 16进制字符串 
     * @throws 
     */  
    public static String string2HexString(String strPart) {  
        StringBuffer hexString = new StringBuffer();  
        for (int i = 0; i < strPart.length(); i++) {  
            int ch = (int) strPart.charAt(i);
            String strHex = StringUtil.padLeft(Integer.toHexString(ch), 2, '0');  
            hexString.append(strHex);  
        }  
        return hexString.toString();  
    }  
    
    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();
    }
  
    /** 
     * @Title:hexString2String 
     * @Description:16进制字符串转字符串 
     * @param src 
     *            16进制字符串 
     * @return 字节数组 
     * @throws 
     */  
    public static String hexString2String(String src) {  
        String temp = "";  
        for (int i = 0; i < src.length() / 2; i++) {  
            temp = temp  
                    + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),  
                            16).byteValue();  
        }  
      
        return temp;  
    }  
    
    /** 
     * @Title:char2Byte 
     * @Description:字符转成字节数据char-->integer-->byte 
     * @param src 
     * @return 
     * @throws 
     */  
    public static Byte char2Byte(Character src) {  
        return Integer.valueOf((int)src).byteValue();  
    }  
      
        /** 
     * @Title:intToHexString 
     * @Description:10进制数字转成16进制 
     * @param a 转化数据 
     * @param len 占用字节数 
     * @return 
     * @throws 
     */  
   public  static String intToHexString(int a,int len){  
        len<<=1;  
        String hexString = Integer.toHexString(a);  
        int b = len -hexString.length();  
        if(b>0){  
            for(int i=0;i<b;i++)  {  
                hexString = "0" + hexString;  
            }  
        }  
        return hexString;  
    }  
    
    /** 
     * @Title:bytes2HexString 
     * @Description:字节数组转16进制字符串 
     * @param b 
     *            字节数组 
     * @return 16进制字符串 
     * @throws 
     */  
    public static String bytes2HexString(byte[] b) {  
        StringBuffer result = new StringBuffer();  
        String hex;  
        for (int i = 0; i < b.length; i++) {  
            hex = Integer.toHexString(b[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
            }  
            result.append(hex.toUpperCase());  
        }  
        return result.toString();  
    }  
  
    /** 
     * @Title:hexString2Bytes 
     * @Description:16进制字符串转字节数组 
     * @param src 
     *            16进制字符串 
     * @return 字节数组 
     * @throws 
     */  
    public static byte[] hexString2Bytes(String src) {  
        int l = src.length() / 2;  
        byte[] ret = new byte[l];  
        for (int i = 0; i < l; i++) {  
            ret[i] = (byte) Integer  
                    .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();  
        }  
        return ret;  
    }  
  
    
    /**
     * Convert byte[] to hex
     * string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)来转换成16进制字符串。
     * @param src byte[] data
     * @return hex string
     */
    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();
    }


    /**
     * Convert byte[] to hex
     * string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)来转换成16进制字符串。
     * @param src byte[] data
     * @return hex string
     */
    public static String bytesToHexString(byte[] src, int len) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < len; 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进制字符串转换成字节数组
     * @param hexString
     * @return byte[]
     */
    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (charToByte(achar[pos]) << 4 | charToByte(achar[pos + 1]));
        }
        return result;
    }


    /**
     * Convert hex string to byte[]
     * @param hexString the hex string
     * @return byte[]
     */
    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;
    }

	public static String convertStringToHex(String str) {

		char[] chars = str.toCharArray();

		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			hex.append(Integer.toHexString((int) chars[i]));
		}

		return hex.toString();
	}

    /**
     * 转化十六进制编码为字符串
     * @param s
     * @return
     */
    public static String toStringHex2(String s) {
        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");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }


    /**
     * Convert char to byte
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }


   
    public final static char[] BToA = "0123456789abcdef".toCharArray();


    public static byte[] asciiToBcd(byte[] ascii, int asc_len) {
        byte[] bcd = new byte[asc_len / 2];
        int j = 0;
        for (int i = 0; i < (asc_len + 1) / 2; i++) {
            bcd[i] = ascToBcd(ascii[j++]);
            bcd[i] = (byte) (((j >= asc_len) ? 0x00 : ascToBcd(ascii[j++])) + (bcd[i] << 4));
        }
        return bcd;
    }


    private static byte ascToBcd(byte asc) {
        byte bcd;

        if ((asc >= '0') && (asc <= '9'))
            bcd = (byte) (asc - '0');
        else if ((asc >= 'A') && (asc <= 'F'))
            bcd = (byte) (asc - 'A' + 10);
        else if ((asc >= 'a') && (asc <= 'f'))
            bcd = (byte) (asc - 'a' + 10);
        else
            bcd = (byte) (asc - 48);
        return bcd;
    }


    /**
     * @函数功能: BCD码转ASC码
     * @输入参数: BCD串
     * @输出结果: ASC码
     */
    public static String bcd2Asc(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            int h = ((bytes[i] & 0xf0) >>> 4);
            int l = (bytes[i] & 0x0f);
            temp.append(BToA[h]).append(BToA[l]);
        }
        return temp.toString();
    }


    public static short getShort(byte[] bytes) {
        return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
    }


    public static int getInt(byte[] bytes) {
        return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))
            | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
    }


    /**
     * @功能: BCD码转为10进制串(阿拉伯数据)
     * @参数: BCD码
     * @结果: 10进制串
     */
    public static String bcd2Str(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp.toString()
            .substring(1) : temp.toString();
    }


    /**
     * @功能: 10进制串转为BCD码
     * @参数: 10进制串
     * @结果: BCD码
     */
    public static byte[] str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }
            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }
    
 // 转化十六进制编码为字符串
    public static String toStringHex3(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (Integer.parseInt(
                    s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "utf-8");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
    
    
	public static String HexToAsciiString(String hex) {

		StringBuilder sb = new StringBuilder();

		// 49204c6f7665204a617661 split into two characters 49, 20, 4c...
		for (int i = 0; i < hex.length() - 1; i += 2) {

			// grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			// convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			// convert the decimal to character
			sb.append((char) decimal);

		}
		 return sb.toString();
	}
    
    public static byte[] HexStrToAscii(String InString)
    {
        int i = 0;
        byte[] by = new byte[InString.length()/2];
        byte temp = 0;
        int loop = 0;
        // 字符转为byte
        while (true)
        {
            temp = (byte) Integer.parseInt(InString.substring(i, i + 2), 16);
            //
            by[loop] = temp;
            i = i + 2;
            loop++;
            if (i >= InString.length())
            {
                break;
            }
            
        }
       
        return by;
    }


    // 601234567860210000000008200000000000c00010
    // 451230124512301200000210301
    // 00526012345678602100 00000008200000000000C00010
    // 34353132333031323132333435363738393031323334350011000002103010
    /**
     * 位图中的 2进制字符串 转hex 字符串
     * @param 2进制字符串
     * @return
     */
    public static String binaryString2hexString(String binaryString) {
        if (binaryString == null || binaryString.length() % 2 != 0)
            return null;
        String hexString = "", tmp;
        for (int i = 0; i < binaryString.length() / 4; i++) {
            tmp = Integer.toHexString(
                Integer.parseInt(binaryString.substring(i * 4, (i + 1) * 4), 2));
            hexString += tmp.substring(tmp.length() - 1);
        }
        return hexString;
    }

    
    public static String convertAsciiToHex(String str) {

        char[] chars = str.toCharArray();

        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }

        return hex.toString();
    }

    /**
     * 字符串的长度不足，补0固定字长
     * @param str 字符串
     * @param strLength 字符串固定长度
     * @param left true左补0，false右补0
     * @return
     */
    public static String addZeroForNum(String str, int strLength, boolean left) {
        int strLen = str.length();
        StringBuffer sb = null;
        while (strLen < strLength) {
            sb = new StringBuffer();
            if (left) {
                sb.append("0").append(str); // 左补0
            } else {
                sb.append(str).append("0");// 右补0
            }
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }


    /**
     * 类型格式化 被转换数据16进制
     * @param type
     * @param data 16进制
     * @return
     */
  /*  public static String formatData(String type, String data) {

        if ("BCD".equals(type)) {
            return data;
        } else if ("ASCII".equals(type)) {
            return ConvertUtil.toStringHex3(data);
        } else if ("BINARY".equals(type)) {
            return hexString2binaryString(data);
        } else {
            return data;
        }
    }*/


    /**
     * 类型格式化 返回数据为16进制
     * @param type 被转换的数据类型
     * @param data 被转换的数据
     * @return
     */
    public static String formatToHexData(String type, String data) {

        if ("BCD".equals(type)) {
            return data;
        } else if ("ASCII".equals(type)) {
            return ConvertUtil.string2HexString(data);
        } else if ("BINARY".equals(type)) {
            return binaryString2hexString(data);
        } else {
            return data;
        }
    }
    
    
    /**
     * 
     * @param data
     */
    public static String doHexToIntStr(String tmp) {
    	return String.valueOf(Integer.parseInt(tmp, 16));
    }
}
