Conversion.java 3.14 KB
package android.zyapi;



import java.io.ByteArrayOutputStream;

public class Conversion {
	/* 
	 * 16进制数字字符
	 */ 
	private static String hexString="0123456789ABCDEF"; 

	public static String encode(String str) 
	{ 

		byte[] bytes=str.getBytes(); 
		StringBuilder sb=new StringBuilder(bytes.length*2); 

		for(int i=0;i<bytes.length;i++) 
		{ 
			sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); 
			sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); 
		} 
		return sb.toString(); 
	} 



	public static String decode(String bytes) 
	{ 
		ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); 

		for(int i=0;i<bytes.length();i+=2) 
			baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)))); 
		return new String(baos.toByteArray()); 
	} 

	public static byte uniteBytes(byte src0, byte src1) {   
		byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();   
		_b0 = (byte)(_b0 << 4);   
		byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();   
		byte ret = (byte)(_b0 ^ _b1);   
		return ret;   
	}    

	public static byte[] HexString2Bytes(String src){   
		byte[] ret = new byte[src.length()/2];   
		byte[] tmp = src.getBytes();   
		for(int i=0; i< tmp.length/2; i++){   
			ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);   
		}   
		return ret;   
	}   


	public static void printHexString(String hint, byte[] b) {   
		System.out.print(hint);   
		for (int i = 0; i < b.length; i++) {   
			String hex = Integer.toHexString(b[i] & 0xFF);   
			if (hex.length() == 1) {   
				hex = '0' + hex;   
			}   
			System.out.print(hex.toUpperCase() + " ");   
		}   
		System.out.println("");   
	}   

	public static String Bytes2HexString(byte[] b) {   
		String ret = "";   
		for (int i = 0; i < b.length; i++) {   
			String hex = Integer.toHexString(b[i] & 0xFF);   
			if (hex.length() == 1) {   
				hex = '0' + hex;   
			}   
			ret += hex.toUpperCase();   
		}   
		return ret;   
	}  


	public static String oneBytes2HexString(byte b) {   
		String ret = "";   
		String hex = Integer.toHexString(b & 0xFF);   
		if (hex.length() == 1) {   
			hex = '0' + hex;   
		}   
		ret += hex.toUpperCase();     
		return ret;   
	}




	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();  
	}  


    public static String bytesToHexString(byte[] src) {  
        StringBuilder stringBuilder = new StringBuilder("0x");  
        if (src == null || src.length <= 0) {  
            return null;  
        }  
        char[] buffer = new char[2];  
        for (int i = 0; i < src.length; i++) {  
            buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);  
            buffer[1] = Character.forDigit(src[i] & 0x0F, 16);  
            //System.out.println(buffer); 
            stringBuilder.append(buffer);  
        }  
        return stringBuilder.toString();  
    }
}